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