Java emails with files

Source: Internet
Author: User

In the case of simple email sending with file upload, this case has to be imported into the mail. jar package, which can be downloaded on Baidu.

Note the following:

①: Name --> it's just a name. Do not include @ or anything,

②: Smtp address: QQ mailbox --> smtp.qq.com

163 email --> smtp.163.com

126 email --> smtp.126.com

Yeah email --> smtp.yeah.net

That is, smtpServer Parameters

 

Package com. email;

Import javax. mail .*;

Import javax. mail. internet .*;

Import java. util .*;

Import javax. activation .*;

Public class BaseMail {

Private int count = 1;

Transport transport = null;

/*** Send an email

* @ Param smtp server example: smtp.exmail.qq.com

* @ Param User Name

* @ Param Password

* @ Param sender's email address

* @ Param recipient's email address

* @ Param mail title

* @ Param Message Body

* @ Param attachment address

*/

Public boolean sendMail (String smtpServer, String name, String password, String meMail, String toMail, String mailTitle, String mailText String fileName ){

Long begin = System. currentTimeMillis ();

Properties props = System. getProperties (); // set the smtp server

Props. setProperty ("mail. smtp. host", smtpServer); // Most smpts need to be verified.

Props. put ("mail. smtp. auth", "true ");

Session s = Session. getInstance (props); // to view runtime information

// Create a message object by the mail session

MimeMessage message = new MimeMessage (s );

Try {

// Sender

InternetAddress from = new InternetAddress (meMail );

Message. setFrom (from );

// Recipient

InternetAddress to = new InternetAddress (toMail );

Message. setRecipient (Message. RecipientType. TO, );

// Mail title

Message. setSubject (mailTitle );

// String content = "Test content ";

// The Mail content, or the plain text "text/plain"

// Message. setContent (content, "text/html; charset = GBK ");

// The following code sends an attachment

// String fileName = "d: // hello.txt ";

MimeBodyPart messageBodyPart = new MimeBodyPart ();

MessageBodyPart. setText (mailText );

Multipart multipart = new MimeMultipart ();

Multipart. addBodyPart (messageBodyPart );

MessageBodyPart = new MimeBodyPart ();

// Upload a file

If (null! = FileName ){

DataSource source = new FileDataSource (fileName );

MessageBodyPart. setDataHandler (new DataHandler (source ));

MessageBodyPart. setFileName (fileName. substring (fileName. lastIndexOf ("/") + 1 ));

Multipart. addBodyPart (messageBodyPart );

}

Message. setContent (multipart );

Message. saveChanges (); transport = s. getTransport ("smtp"); // smtp verification, that is, the user name and password of the email you use to send emails

Transport. connect (smtpServer, name, password); // send

Transport. sendMessage (message, message. getAllRecipients ());

Return true;

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

Try {

Transport. close ();

} Catch (MessagingException e ){

E. printStackTrace ();

}

}

Return false;

}

Public static void main (String [] args ){

BaseMail mail = new BaseMail ();

Mail. sendMail ("smtp.yeah.net", "12345", "password", "12345@yeah.net", "recipient's email address", "title", "content ", "file address ");}

}

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.