C#實現自動發郵件

iteye_20683發表於2009-12-17

出處:twanger.cublog.cn 作者:twanger 時間:2007-3-14 19:31:05

public void send(MailMsg msg, string[] attachments)
{
putlog("send:1");
MailMessage mail = new MailMessage();
mail.To = msg.MailTo;
mail.From = msg.MailFrom;

mail.Subject = msg.MailSubject;
mail.Body = msg.MailContent;
mail.BodyFormat = MailFormat.Html;

for (int i = 0; i {
putlog("send:1-" + (i + 1).ToString());
if (attachments[i] != null && attachments[i].ToString() != "")
{
putlog("send:1-" + (i + 1).ToString() + "-1");
MailAttachment attachment = new MailAttachment(attachments[i].ToString());
mail.Attachments.Add(attachment);
}
}

putlog("send:2");

//basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", ConfigurationManager.AppSettings["UserName"]);
//set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ConfigurationManager.AppSettings["UserPwd"]);

//ip of mail server
SmtpMail.SmtpServer = ConfigurationManager.AppSettings["mailServerIP"]; //"10.25.100.21"; //your real server goes here
SmtpMail.Send(mail);
}

相關文章