<span id= "more-1794" ></span> Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Net.Mail; Using System.Net.Mime; Using System.IO; Using System.Timers; Using System.Xml; Using System.Net; Namespace TEST. Subunit { public class Emailhelper { Public MailMessage mailmessage (string fromaddress,string desemailuser, String toaddress, String emailSubject, String Emailtext)//Mail sender, support for mass, multiple addresses separated by a half-width comma { MailMessage mm = new MailMessage (); Mm. To.add ("abc@qq.com"); Mm. To.add (toaddress)//temporary shielding Cc Mm. Cc. ADD (New MailAddress ("11@qq.com", "John", Encoding.GetEncoding (936)); Mm. Cc. ADD (New MailAddress ("22@qq.com", "Dick", Encoding.GetEncoding (936)); Mm. Cc. ADD (New MailAddress ("33@126.com", "Harry", Encoding.GetEncoding (936)); Mm. Bcc.add ("44@qq.com,44@qq.com"); Mm. from = new MailAddress (fromaddress, Desemailuser. Length>0?desemailuser:fromaddress, Encoding.GetEncoding (936)); Mm. subjectencoding = encoding.getencoding (936);//Here is very important, if your mail title contains Chinese, here must be specified, otherwise the other side received very likely garbled. Mm. Subject = EmailSubject; Message headers Mm. Isbodyhtml = true; Whether the message body is HTML format Mm. bodyencoding = encoding.getencoding (936); The message body code, set incorrectly, the receiver will receive garbled //------------------------------------------------------------------------- Emailtext + = "This is the mailbox to be sent correctly:" + toaddress; Mm. BODY = emailtext;//Message text Mm. Priority = Mailpriority.high; Message priority, divided into low, normal, high, usually with normal Mm. Attachments.Add (New Attachment (@ "D:a.doc", SYSTEM.NET.MIME.MEDIATYPENAMES.APPLICATION.RTF)); The second parameter, which represents the file type of the attachment, can be used without specifying return mm; } public bool SendEmail (string fromemailaddress, String toemailaddress,string emailtitle,string emailcontent,string Host , String port,string fromemailuser,string desemailuser,string fromemailpass) { BOOL B=false; String message=string. Empty; MailMessage mailessage =this.mailmessage (fromemailaddress,desemailuser,toemailaddress,emailtitle,emailcontent); SmtpClient smtp = new SmtpClient (); Instantiate a SmtpClient Smtp. Deliverymethod = Smtpdeliverymethod.network; Set SMTP Outbound to Network Smtp. Enablessl = FALSE;//SMTP Whether SSL encryption is enabled on the server Smtp. host = host; Specify SMTP server address Smtp. Port = Int. Parse (port); Specifies the port of the SMTP server, which defaults to 25 Smtp. Credentials = new NetworkCredential (Fromemailuser, fromemailpass);//Certification Try { Smtp. Send (Mailessage); b = True; Message= "Send success!" } catch (System.Net.Mail.SmtpException ex) { b = False; Message= "Send failed!"; } return b; } } } |