Use JavaMail to send mail (Gmail) __java

Source: Internet
Author: User
Tags getmessage valid email address gmail password


Take the Gmail server as an example

Enter the correct username and password, and then enter a valid email address to deliver .

The code has been validated through.



Below is the source code

Package com.suifeng.mail;

Import java.util.Properties;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;

Import Javax.mail.internet.MimeMessage;
    public class Javaemail {Properties emailproperties;
    Session mailsession;

    MimeMessage Emailmessage;

        public static void Main (String args[]) {javaemail javaemail = new Javaemail ();
        Javaemail.setmailserverproperties ();
            try {javaemail.createemailmessage ();
        Javaemail.sendemail ();
            catch (Addressexception e) {System.out.println ("address Exception:" + e.getmessage ());
        E.printstacktrace ();
            catch (Messagingexception e) {System.out.println ("message Exception:" + e.getmessage ());
        E.printstacktrace ();
    }public void Setmailserverproperties () {String emailport = "587";//Gmail ' s SMTP port EMAILPR
        Operties = System.getproperties ();
        Emailproperties.put ("Mail.smtp.port", Emailport);
        Emailproperties.put ("Mail.smtp.auth", "true");

    Emailproperties.put ("Mail.smtp.starttls.enable", "true"); public void Createemailmessage () throws Addressexception, messagingexception {string[] ToE
        mails = {"Mailboxes to send"};
        String emailSubject = "Java Email";

        String Emailbody = "This is a email sent by <b>JavaMail</b> API.";
        MailSession = session.getdefaultinstance (emailproperties, NULL);

        Emailmessage = new MimeMessage (mailsession);
                    for (int i = 0; i < toemails.length i++) {emailmessage.addrecipient (Message.RecipientType.TO,
        New InternetAddress (Toemails[i]));
       } emailmessage.setsubject (EmailSubject); Emailmessage.setcontent (Emailbody, "text/html");/for an HTML email//Emailmessage.settext (emailbody);/For a T Ext email} public void SendEmail () throws Addressexception, messagingexception {String emailhost
        = "smtp.gmail.com";  String Fromuser = "Here's your Gmail email address";//Just the ID alone without//@gmail. com string Fromuseremailpassword

        = "Make your Gmail password here";

        Transport transport = Mailsession.gettransport ("SMTP");
        Transport.connect (Emailhost, Fromuser, Fromuseremailpassword);
        Transport.sendmessage (Emailmessage, emailmessage.getallrecipients ());
        Transport.close ();
    System.out.println ("Email sent successfully.");
 }

}


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.