搜尋此網誌
2011年12月21日 星期三
C# 寄信語法(System.Net.Mail)
using System.Net.Mail;
public void Send2(string szTo, string szSubject, string szBody, string szFrom, FileUpload Attachmentsurl)
{
//szTo 收件者 szSubject主旨 szBody內容 szFrom 寄件者 Attachmentsurl 附件
object[] mailtoadd = szTo.Split(',');
for (int i = 0; i <= mailtoadd.Length - 1; i++)
{
MailAddress from = new MailAddress("寄件者", "收到後要顯示的名稱", System.Text.Encoding.UTF8);
MailAddress to = new MailAddress(mailtoadd[i].ToString());
MailMessage em = new MailMessage(from, to);
em.SubjectEncoding = System.Text.Encoding.UTF8;
em.BodyEncoding = Encoding.UTF8;
//信件主題
em.Subject = szSubject;
//內容
em.Body = szBody;
if (Attachmentsurl.FileName != "")
{
string path = @"" + 路徑 + Attachmentsurl.FileName.ToString();
Attachment data = new Attachment(path);
em.Attachments.Add(data);
}
em.IsBodyHtml = true;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
//登入帳號認證
client.Credentials = new System.Net.NetworkCredential("帳號", "密碼");
// port 跟 host 是用gmail做例子其他非一樣
client.Port = 587;
client.Host = "smtp.gmail.com";
//啟動SSL
client.EnableSsl = true;
try
{
client.Send(em);
}
catch (Exception e)
{
}
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言