您的位置:首页 > 运维架构

OpenSmtp 发送邮件

2013-12-04 17:42 399 查看
1.采用发送一个简单邮件

示例:

private int smtpPort;
private string smtpHost;
private int recieveTimeout;
private int sendTimeout;

private string subject;
private string body;
private StringBuilder htmlBody;

private Smtp smtp;
private MailMessage msg;
private EmailAddress senderAddress;
private EmailAddress replyToAddress;
private EmailAddress recipientAddress;
private EmailAddress ccAddress;
private EmailAddress bccAddress;
subject = "Test Subject 哈哈 International: ËÇÅÃÄÄÅÂÀèéêëìí";
body = "Hello Jane.\r\n This is the body of the mail message. \r\nInternational chars: ËÇÅÃÄÄÅÂÀèéêë\r\nìíîïñaÿc";
htmlBody = new StringBuilder();
htmlBody.Append("<HTML><HEAD></HEAD><BODY bgColor=\"#00ffff\"><b>Hello Jane. This is the body of the HTML mail message. International chars: ËÇÅÃÄÄÅÂÀèéêëìíîïñaÿc</b></BODY></HTML>");
senderAddress = new EmailAddress("sulin11026@163.com", "John Sender");
recipientAddress = new EmailAddress("sulin@moneybrace.com", "Jane Doe");
ccAddress = new EmailAddress("ccAddress@localhost", "CC Name");
bccAddress = new EmailAddress("bccAddress@localhost", "BCC Name");

msg = new MailMessage(senderAddress, recipientAddress);
smtpHost = "smtp.163.com";
smtpPort = 25;
smtp = new Smtp();
smtp.Host = smtpHost;
smtp.Port = smtpPort;
msg.Subject = subject;
msg.Body = body;
msg.AddImage(Server.MapPath("context/test.jpg"), "testimage");
msg.AddImage(Server.MapPath("omnipay.jpg"), "omnipay");
msg.HtmlBody = "<body><table><tr><td><b>Here is an embedded IMAGE:<img src=\"cid:testimage\"></td></tr>\r\n<tr><td>Here's another: <img src=\"cid:omnipay\"></td></tr></table></body>";
//htmlbody 会覆盖body 的内容
msg.AddRecipient(ccAddress, AddressType.Cc);
msg.AddRecipient(bccAddress, AddressType.Bcc);

msg.AddAttachment(Server.MapPath("context/test.jpg"));
msg.AddAttachment(new Attachment(new FileStream(Server.MapPath("context/test.htm"), FileMode.Open, FileAccess.Read), "test.html"));

msg.AddCustomHeader("X-FakeTestHeader", "Fake Value");
msg.AddCustomHeader("X-AnotherFakeTestHeader", "Fake Value");
msg.Notification = true;
msg.Charset = "UTF-8";
msg.Priority = MailPriority.Low;

smtp.Username = "sulin11026@163.com";
smtp.Password = "**********";

smtp.SendMail(msg);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: