Automatically send emails

Source: Internet
Author: User
Tags mailmessage smtpclient

1. Configure the web. config file first:
<System.net>
<MailSettings>
<Smtp from = "Emailname">
<Network host = "smtp.163.com" userName = "Emailname" password = "Emailpassword"
Port = "25" defaultCredentials = "false"/>
</Smtp>
</MailSettings>
</System.net> 2. Compile the function for sending emails:
/** // <Summary>
/// Email sending method (with attachment)
/// </Summary>
/// <Param name = "mailto"> recipient address. For example, mailto: receiver@163.com % 3C/param>
/// <Param name = "mailsubject"> email title </param>
/// <Param name = "mailbody"> email body </param>
/// <Param name = "mailFrom"> email recipient address. For example, mailto: sender@163.com % 3C/param>
/// <Param name = "list"> attachment path </param>
/// <Returns> </returns>
Public bool MySendMail (string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
...{
Try
...{
// Email recipient address
System. Net. Mail. MailAddress from = new System. Net. Mail. MailAddress (mailFrom );
// Such as test@163.com, preliminary test, can not use the test@sina.com, with 163 mail server, you must use 163 mailbox User Name
// Recipient address
System. Net. Mail. MailAddress to = new System. Net. Mail. MailAddress (mailto); // such as test@tom.com
System. Net. Mail. MailMessage mail = new System. Net. Mail. MailMessage (from, );
Mail. Subject = mailsubject;
Mail. Body = mailbody;

// Set the server as follows
System. Net. Mail. SmtpClient mySmth = new System. Net. Mail. SmtpClient ();
// Add attachments as follows
Int count = list. Count;
For (int I = 0; I <count; I ++)
...{
System. Net. Mail. Attachment data = new System. Net. Mail. Attachment (list [I]. ToString ());
Mail. Attachments. Add (data );
}
MySmth. Send (mail );
Mail. Dispose ();
Return true;
}
Catch
...{
Return false;

}
}
3. Finally, call the function:

// Automatically send the email
String mailSubject = "Member registration confirmation letter ";
String mailBody = "body content. ";
String mailFrom = ConfigurationManager. receivetask[ "SendMail"];
ArrayList List = new ArrayList ();
List. Add (Server. MapPath (ConfigurationManager. receivetask[ "SendMailText"]);
If (MySendMail(this.txt Email. Text, mailSubject, mailBody, mailFrom, List ))
...{
ShowAndRedirect (this, "the registration confirmation letter has been sent to your mailbox. Please check it! "," Index. aspx ");
}
Else
...{
ShowAndRedirect (this, "email sending failed! "," Index. aspx ");
Return;
}

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/h1jie/archive/2008/01/21/2057212.aspx

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.