|
復(fù)制代碼 代碼如下:
public static int sendmail(string to, string body,string subject)
{
try
{
int nContain = 0;
///添加發(fā)件人地址
string from = "你的發(fā)送EMAIL";
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(from);
nContain += mailMsg.From.Address.Length;
///添加收件人地址
mailMsg.To.Add(to);
nContain += mailMsg.To.ToString().Length;
///添加郵件主題
mailMsg.Subject = subject;
mailMsg.SubjectEncoding = Encoding.UTF8;
nContain += mailMsg.Subject.Length;
///添加郵件內(nèi)容
mailMsg.Body = body;
mailMsg.BodyEncoding = Encoding.UTF8;
mailMsg.IsBodyHtml = true;
nContain += mailMsg.Body.Length;
if (mailMsg.IsBodyHtml == true)
{
nContain += 100;
}
///發(fā)送郵件
try
{
//定義發(fā)送郵件的Client
SmtpClient client = new SmtpClient();
//表示以當(dāng)前登錄用戶的默認(rèn)憑據(jù)進(jìn)行身份驗(yàn)證
client.UseDefaultCredentials = true;
//包含用戶名和密碼
client.Credentials = new System.NET.NETworkCredential(application.GetapplicationSendmail(), application.GetapplicationSendpass());
///設(shè)置郵件服務(wù)器主機(jī)的IP地址
client.Host = "SMTP服務(wù)器IP"
///設(shè)置郵件服務(wù)器的端口
client.Port = 25;
///配置發(fā)送郵件的屬性
client.DeliveryMethod = SmtpDeliveryMethod.NETwork;
//System.NET.Mail.MailMessage message = new System.NET.Mail.MailMessage(strFrom, strto, strSubject, strBody);
mailMsg.Priority = System.NET.Mail.MailPriority.Normal;
//client.UseDefaultCredentials = false;
///發(fā)送郵件
client.Send(mailMsg);
return 1;
}
catch (Exception ex) { return 0; }
}
catch (Exception ex) { return 0; }
}
AspNet技術(shù):C#中發(fā)送郵件代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。