Java source code for sending mail

Source: Internet
Author: User

Package com.test.mail;

Import java.util.Properties;

Import Javax.mail.Authenticator;
Import Javax.mail.BodyPart;
Import Javax.mail.Message;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;

public class SendMail {
public static void Main (string[] args) {
try {
String username= "source@163.com";
String password= "xxxxx";
String smtp_server= "smtp.163.com";
String From_mail_address=username;
String to_mail_address= "to@163.com";

Authenticator auth = new Popupauthenticator (Username,password);

Properties Mailprops = new properties ();
Mailprops.put ("Mail.smtp.auth", "true");

Mailprops.put ("username", username);

Mailprops.put ("password", password);

Mailprops.put ("Mail.smtp.host", smtp_server);
Session mailsession = Session.getdefaultinstance (Mailprops, auth);
MimeMessage message = new MimeMessage (mailsession);
Message.setfrom (New InternetAddress (from_mail_address));
Message.setrecipient (Message.RecipientType.TO, New InternetAddress (to_mail_address));
Message.setsubject ("Mail Test");

Mimemultipart multi = new Mimemultipart ();
BodyPart Textbodypart = new MimeBodyPart ();
Textbodypart.settext ("e-mail test content!");
Multi.addbodypart (Textbodypart);
Message.setcontent (multi);
Message.savechanges ();
Transport.send (message);
catch (Exception ex) {
System.err.println ("The reason for the mail failure is:" + ex.getmessage ());
SYSTEM.ERR.PRINTLN ("Specific error Reason:");
Ex.printstacktrace (System.err);
}
}

}

Class Popupauthenticator extends Authenticator {
 private String username;
 private String password;
 public popupauthenticator (String username,string password) {
  this.username=username;
     This.password=password;
 }
 public passwordauthentication getpasswordauthentication () {
  return new Passwordauthentication (This.username, This.password);
 }
} 

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.