C # mail C # sendemail

Source: Internet
Author: User
<? XML version = "1.0" encoding = "UTF-8"?> <Config> <fromadd> zhangyongbin2814@163.com </fromadd> <fromname> Zhang yongbin </fromname> <toname> jiejisdilesaway@163.com; 99792334@qq.com </toname> <smtpservername> smtp.163.com </smtpservername> <sendname> zhangyongbin2814@163.com </sendname> <sendpwd> asdfasdsdged331sd </sendpwd> </config>

  

  

public static void getMailConfig(string mailConfigPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(mailConfigPath);
XmlNode FromAddRoot = xmlDoc.SelectSingleNode("config/FromAdd");
XmlNode FromNameRoot = xmlDoc.SelectSingleNode("config/FromName");
XmlNode ToNameRoot = xmlDoc.SelectSingleNode("config/ToName");
XmlNode SmtpServerNameRoot = xmlDoc.SelectSingleNode("config/SmtpServerName");
XmlNode SendNameRoot = xmlDoc.SelectSingleNode("config/SendName");
XmlNode SendPwdRoot = xmlDoc.SelectSingleNode("config/SendPwd");
fromAdd = FromAddRoot.InnerText.Trim().ToString();
fromName = FromNameRoot.InnerText.Trim().ToString();
toName = ToNameRoot.InnerText.Trim().ToString();
smtpServerName = SmtpServerNameRoot.InnerText.Trim().ToString();
sendName = SendNameRoot.InnerText.Trim().ToString();
sendPwd = SendPwdRoot.InnerText.Trim().ToString();
}

Public static void sendemail (string mailtitle, string mailcontent) {mailaddress from = new mailaddress (fromadd, fromname); // mail sender mailmessage mail = new mailmessage (); // set the mail title. subject = mailtitle; // set the sender of the email. // pass: if you do not want to display your email address, enter any name in the mail format, the user who is sending mail is not set here. This is only for displaying and using mail. from = from; // set the recipient string address = ""; string displayname = "";/* this is written here because it may be sent to multiple contacts, each address is used; IDs are usually directly selected from the address book. When selecting a contact, the format is: username 1 <mail1>; username 2 <Mail 2>; therefore, the following piece of code with poor logic is simple if you always need to send only one recipient. to. add ("recipient mail"); */string [] mailnames = (toname + ";"). split (';'); foreach (string name in mailnames) {If (name! = String. empty) {If (name. indexof ('<')> 0) {displayname = Name. substring (0, name. indexof ('<'); Address = Name. substring (name. indexof ('<') + 1 ). replace ('>', '');} else {displayname = string. empty; address = Name. substring (name. indexof ('<') + 1 ). replace ('>', '');} mail. to. add (New mailaddress (address, displayname) ;}// it's much easier to set the CC recipient for an email, if you do not want to get laid off an important file quickly, you can send a CC copy to the lead. // mail. cc. add (New mailadd RESS (ccnames, "Dear Leader"); // set mail content. body = mailcontent; // set the mail format. bodyencoding = system. text. encoding. utf8; mail. isbodyhtml = true; // sets the mail sending level. priority = mailpriority. normal; // set the email attachment. Upload the attachment selected on the client to the server and save it. Then add it to mail. // string filename = txtupfile. postedfile. filename. trim (); // filename = "D:/upfile/" + filename. substring (filename. lastindexof ("/") + 1); // txtupfile. postedfile. saveas (Filename); // save the file to the server // mail. attachments. add (new attachment (filename); mail. deliverynotificationoptions = deliverynotificationoptions. onsuccess; smtpclient client = new smtpclient (smtpservername); // set the name of the host used for SMTP transactions, and enter the IP address. // client. host = "172.18.2.26"; // set the port used for SMTP transactions. The default value is 25 // client. port = 25; client. usedefaultcredentials = false; // here is the real email login name and password. For example, my email address is hbgx @ hotmail, and my username is hbgx. My password is xgbh client. credentials = new system. net. networkcredential (sendname, sendpwd); client. deliverymethod = smtpdeliverymethod. network; // all definitions are complete. The message is officially sent. It's easy! Client. Send (Mail );}

  

Loading editor...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.