Jmail4.3 Method for sending emails with attachments

Source: Internet
Author: User
1 /// <summary>
2 // send the file as an attachment to the email
3 /// </Summary>
4 /// <Param name = "FILENAME"> file path </param>
5 // <Param name = "title"> title </param>
6 /// <returns> whether the message is sent successfully </returns>
7 public bool Sendmail (string filename, String title)
8 {
9 bool sendsuccess = false;
10 try
11 {
12 // all information is taken from the configuration file
13 configsettings setting = Global. getinstance (). settings;
14 // create an email
15 jmail. Message MSG = new jmail. messageclass ();
16 MSG. Logging = true;
17 // email sending Address
18 MSG. From = Setting. getstring (settingkey. mailaddress. tostring ());
19 // sender name
20 msg. fromname = Setting. getstring (settingkey. mailusername. tostring ());
21 // recipient's address
22 string readdress = Setting. getstring (settingkey. mailrecipientsaddress. tostring ());
23 // Recipient Name
24 string rename = (readdress. Split ('@') [0]. Trim ();
25 // Add a recipient to the email
26 msg. addrecipient (readdress, rename ,"");
27 // subject
28 MSG. Subject = title;
29 // Encoding
30 msg. charset = "gb2312 ";
31 // content
32 msg. Body = "";
33 // Add an attachment
34 msg. addattachment (filename, true ,"");
35 // obtain the sending information
36 string username = Setting. getstring (settingkey. mailusername. tostring ());
37 string Password = Setting. getstring (settingkey. mailpassword. tostring ());
38 string SMTP = Setting. getstring (settingkey. mailsmtpserveraddress. tostring ());
39 // sending information format: sender login mailbox User name: Login Password @ SMTP address (such as a: B @smtp.sohu.com)
40 string sendinfo = username. Trim () + ":" + password. Trim () + "@" + SMTP. Trim ();
41 // send
42 MSG. Send (sendinfo, false );
43 sendsuccess = true;
44}
45 catch (exception ex)
46 {
47 sendsuccess = false;
48 throw ex;
49
50}
51
52 return sendsuccess;
53
54}

Note: Add a reference file: Using jmail; version 4.3

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.