public void TestAttachmentSort() { // test MailMessage(string, string) constructor MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); // test MailMessage(EamilAddress, EmailAddress) constructor EmailAddress from = new EmailAddress("*****@*****.**", "Fake Sender"); EmailAddress to = new EmailAddress("*****@*****.**", "Fake Recipient"); Attachment att = new Attachment(@"..\lib\test attachments\test.htm"); msg.AddAttachment(att); Attachment att2 = new Attachment(@"..\lib\test attachments\test.gar"); msg.AddAttachment(att2); Attachment att3 = new Attachment(@"..\lib\test attachments\test.zip"); msg.AddAttachment(att3); Attachment att4 = new Attachment(@"..\lib\test attachments\test.longextension"); msg.AddAttachment(att4); ArrayList attachments = msg.Attachments; attachments.Sort(); Console.WriteLine("\r\n ----- MailMessage.Attachments after sorting -----"); foreach (Attachment attachment in attachments) { Console.WriteLine(attachment.Name); } }
public void TestAllConstructors() { try { // test MailMessage(string, string) constructor MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); // test MailMessage(EamilAddress, EmailAddress) constructor EmailAddress from = new EmailAddress("*****@*****.**", "Fake Sender"); EmailAddress to = new EmailAddress("*****@*****.**", "Fake Recipient"); msg = new MailMessage(from, to); } catch(SmtpException) { Assertion.Fail("TestAllConstructors threw SmtpException"); } }
public static string SendPassword(string strEmail) { try { string smtpHost = "smtp.163.com"; int smtpPort = 25; string senderEmail = "*****@*****.**"; string senderName = "zhuangrong"; string recipientEmail = strEmail; string subject = "您在PowerSite的密码"; string body = "这是来自PowerSite的密码回复邮件。"; SmtpConfig.VerifyAddresses = false; EmailAddress from = new EmailAddress(senderEmail, senderName); EmailAddress to = new EmailAddress(recipientEmail); MailMessage msg = new MailMessage(from, to); msg.Charset = "gb2312"; msg.Subject = subject; msg.Body = body; Smtp smtp = new Smtp(smtpHost, smtpPort); smtp.Username = "******"; smtp.Password = "******"; smtp.SendMail(msg); return "OK"; } catch (MalformedAddressException mfa) { return mfa.Message.Substring(0, mfa.Message.Length - 4); } catch (SmtpException se) { return se.Message.Substring(0, se.Message.Length - 4); } catch (Exception ex) { return ex.Message.Substring(0, ex.Message.Length - 4); } }
/// <summary> /// 同步发送 /// </summary> /// <param name="subject">主题</param> /// <param name="body">邮件内容</param> /// <param name="attachment">附件</param> /// <param name="receptMail">接收地址[email protected]</param> /// <param name="sendMail">发送邮件地址</param> /// <param name="sendName">发送名称</param> /// <param name="userName">服务器账号</param> /// <param name="password">服务器账户密码</param> /// <param name="smtpHost">SMTP服务器</param> /// <param name="smtpPort">SMTP服务器端口</param> private void SendEmailSync(string subject, string body, string attachment, string receptMail, string sendMail, string sendName, string userName, string password, string smtpHost, int smtpPort) { try { SmtpConfig.VerifyAddresses = false; EmailAddress from = new EmailAddress(sendMail, sendName); EmailAddress to = new EmailAddress(receptMail); MailMessage msg = new MailMessage(from, to); msg.Charset = "gb2312"; msg.Subject = subject; msg.HtmlBody = HttpUtility.HtmlDecode(body); Smtp smtp = new Smtp(smtpHost, smtpPort); // 在SMTP服务器上的用户名和密码 smtp.Username = userName; smtp.Password = password; smtp.SendMail(msg); } catch { } }
protected void Init() { sender = "*****@*****.**"; recipient = "*****@*****.**"; cc = "*****@*****.**"; senderName = "FromName"; recipientName = "ToName"; ccName = "ccName"; subject = "Mail Message Test\r\n"; body = "Hello from MailMessageTest"; htmlBody = "<HTML><HEAD></HEAD><BODY bgColor=\"#00ffff\"><b>Hello Jane. This is the body of the HTML mail message.</b></BODY></HTML>"; charset = "us-ascii"; senderEmail = new EmailAddress(sender, senderName); recipientEmail = new EmailAddress(recipient, recipientName); ccEmail = new EmailAddress(cc, ccName); msg = new MailMessage(senderEmail, recipientEmail); msg.AddRecipient("*****@*****.**", AddressType.To); msg.AddRecipient("*****@*****.**", AddressType.To); msg.Subject = subject; msg.Body = body; msg.Charset = charset; msg.Priority = MailPriority.High; msg.HtmlBody = htmlBody.ToString(); msg.AddRecipient(ccEmail, AddressType.To); msg.AddRecipient(ccEmail, AddressType.Cc); msg.AddCustomHeader("X-Something", "Value"); msg.AddCustomHeader("X-SomethingElse", "Value"); msg.AddAttachment(@"..\lib\test attachments\test.jpg"); msg.AddAttachment(@"..\lib\test attachments\test.htm"); Attachment att = new Attachment(@"..\lib\test attachments\test.zip"); msg.AddAttachment(att); msg.Notification = true; }
public void TestCopy() { // test MailMessage(string, string) constructor MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); // test MailMessage(EamilAddress, EmailAddress) constructor EmailAddress from = new EmailAddress("*****@*****.**", "Fake Sender"); EmailAddress to = new EmailAddress("*****@*****.**", "Fake Recipient"); msg = new MailMessage(from, to); msg.Body = "test body for clone test"; MailMessage msg2 = msg.Copy(); Assertion.AssertEquals(msg.To, msg2.To); Assertion.AssertEquals(msg.From, msg2.From); Assertion.AssertEquals(msg.Body, msg2.Body); }
/// <summary>Resets all of the MailMessage properties</summary> /// <example> /// <code> /// MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); /// msg.Reset(); /// </code> /// </example> public void Reset() { from = null; replyTo = null; recipientList.Clear(); ccList.Clear(); bccList.Clear(); customHeaders.Clear(); attachments.Clear(); subject = null; body = null; htmlBody = null; priority = null; mixedBoundary = null; altBoundary = null; charset = null; notification = false; }
/// <summary>Adds a recipient RFC 822 formatted email address to the MailMessage</summary> /// <param name="address">RFC 822 formatted email address that you want to add to the MailMessage</param> /// <param name="type">AddressType of the email address</param> /// <example> /// <code> /// MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); /// msg.AddRecipient("*****@*****.**", AddressType.Cc); /// </code> /// </example> public void AddRecipient(string address, AddressType type) { EmailAddress email = new EmailAddress(address); AddRecipient(email, type); }
/// <summary>Adds a recipient EmailAddress to the MailMessage</summary> /// <param name="address">EmailAddress that you want to add to the MailMessage</param> /// <param name="type">AddressType of the address</param> /// <example> /// <code> /// MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); /// EmailAddress cc = new EmailAddress("*****@*****.**"); /// msg.AddRecipient(cc, AddressType.Cc); /// </code> /// </example> public void AddRecipient(EmailAddress address, AddressType type) { try { switch(type) { case AddressType.To: recipientList.Add(address); break; case AddressType.Cc: ccList.Add(address); break; case AddressType.Bcc: bccList.Add(address); break; } } catch(Exception e) { throw new SmtpException("Exception in AddRecipient: " + e.ToString()); } }
/// <summary>Constructor using EmailAddress type</summary> /// <example> /// <code> /// EmailAddress from = new EmailAddress("*****@*****.**", "Support"); /// EmailAddress to = new EmailAddress("*****@*****.**", "Joe Smith"); /// MailMessage msg = new MailMessage(from, to); /// </code> /// </example> public MailMessage(EmailAddress sender, EmailAddress recipient) : this() { from = sender; recipientList.Add(recipient); }
protected void Init() { subject = "Test Subject. International chars: ËÇÅÃÄÄÅÂÀèéêëìíîïñaÿc + 功能超强得不得了"; 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("sender@localhost", "John Sender"); recipientAddress = new EmailAddress("administrator@localhost", "Jane Doe"); replyToAddress = new EmailAddress("replyTo@localhost", "ReplyTo Name"); ccAddress = new EmailAddress("ccAddress@localhost", "CC Name"); bccAddress = new EmailAddress("bccAddress@localhost", "BCC Name"); msg = new MailMessage("*****@*****.**", "*****@*****.**"); smtpHost = "localhost"; smtpPort = 25; smtp = new Smtp(); smtp.Host = smtpHost; smtp.Port = smtpPort; // Add Smtp event listener SmtpEventListener listener = new SmtpEventListener(smtp); }