Send directly with JavaMail

Source: Internet
Author: User
Tags mailmessage

Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import javax.mail.internet.MimeUtility;
Import javax.mail.Session;
Import javax.mail.MessagingException;
Import Javax.mail.Transport;

public class Sendhtmlmail {
public void SendMail () {

Create a Mail send class Javamailsender
Used to send basic text message information (cannot include attachments, and pictures)
Javamailsender sender = new Javamailsenderimpl ();


Set up a mail service host
((Javamailsenderimpl) sender). Sethost ("smtp.sohu.com");
The user name of the sender's mailbox
((Javamailsenderimpl) sender). Setusername ("UserName");
Password for sender's mailbox
((Javamailsenderimpl) sender). SetPassword ("password");

Configuration file, for instantiating Java.mail.session
Properties Pro = System.getproperties ();

Login to the SMTP server, need to be authorized, NetEase 163 mailbox recently registered mailbox can not be authorized.
Test Sohu's mailbox for authorization
Pro.put ("Mail.smtp.auth", "true");
Pro.put ("Mail.smtp.socketFactory.port", "25");
Pro.put ("Mail.smtp.socketFactory.fallback", "false");

Get information from a file
((Javamailsenderimpl) sender). setjavamailproperties (PRO);


Create basic message information
MailMessage mailmessage = new Simplemailmessage ();

Sender address, must fill in the correct message format, no person will send failed
Mailmessage.setfrom ("****** @sohu. com");
Message subject
Mailmessage.setsubject ("Test Mail");
Message content, simple message information can only be added text information
Mailmessage.settext ("test");
e-Mail Recipient's email address
Mailmessage.setto ("****** @qq. com");


Send message, parameter can be an array
Sender.send (simplemailmessage[])
Sender.send ((simplemailmessage) mailmessage);

}
}

=============================================================================

public void Sendmimemail () throws messagingexception{

Create a Mail send class Javamailsender
For sending multiple emails, including attachments, images, HTML, etc.
Javamailsender Mimemailsender = new Javamailsenderimpl ();

Set up a mail service host
((Javamailsenderimpl) mimemailsender). Sethost ("smtp.sohu.com");
The user name of the sender's mailbox
((Javamailsenderimpl) mimemailsender). Setusername ("UserName");
Password for sender's mailbox
((Javamailsenderimpl) mimemailsender). SetPassword ("password");

Configuration file, for instantiating Java.mail.session
Properties Pro = System.getproperties ();

Login to the SMTP server, need to be authorized, NetEase 163 mailbox recently registered mailbox can not be authorized.
Test Sohu's mailbox for authorization
Pro.put ("Mail.smtp.auth", "true");
Pro.put ("Mail.smtp.socketFactory.port", "25");
Pro.put ("Mail.smtp.socketFactory.fallback", "false");

((Javamailsenderimpl) mimemailsender). Setjavamailproperties (PRO);

Create a multi-message
MimeMessage mimemessage = ((Javamailsenderimpl) mimemailsender). Createmimemessage ();

Create a MimeMessage helper class for encapsulating information to MimeMessage
Mimemessagehelper helper = new Mimemessagehelper (MimeMessage, True, "GBK");

Basic information

Sender address, must fill in the correct message format, no person will send failed
Helper.setfrom ("******* @sohu. com");
Message subject
Helper.setsubject ("Test Mail");
Message content, simple message information can only be added text information
Helper.settext ("test");
e-Mail Recipient's email address
Helper.setto ("****** @qq. com");


Diversified information

Add HTML information
File Mypicture = new file ("C:\\Documents and settings\\administrator\\ Desktop \\kz2.jpg");

In the case of HTML code, the Boolean parameter is True
Helper.settext ("+ "</a> view <br></body>
Add a picture to the HTML code
Helper.addinline ("Mypicture", mypicture);

Add an attachment
File File = new file ("C:\\Documents and settings\\administrator\\ Desktop \\validate.jar");
Helper.addattachment ("Picture", mypicture);
Helper.addattachment ("Picture", file);

((Javamailsender) mimemailsender). Send (MimeMessage);

SYSTEM.OUT.PRINTLN ("sent successfully");
}

Send directly with JavaMail

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.