JavaMail-based Java Mail sending: Simple Mail sending

Source: Internet
Author: User
Tags imap email account

http://blog.csdn.net/xietansheng/article/details/51673073

Http://www.cnblogs.com/codeplus/archive/2011/10/30/2229391.html

http://blog.csdn.net/ghsau/article/details/17839983

********************

E-mail application is very extensive, for example, a website registered an account, automatically send a welcome email, retrieve the password through the mail, automatic batch send activity information and so on. However, these applications may not be the same as our own usual e-mail, first open the browser, login mailbox, create mail and send. This article will briefly describe what to do if you create an e-mail message by using Java code and connect to the mail server.

1. e-Mail protocol

e-mail is transmitted over the network as well as Web pages that require compliance with specific protocols, and commonly used e-mail protocols include Smtp,pop3,imap. The SMTP protocol is used only for the creation and sending of messages, and all of this is only related to the SMTP protocol. SMTP is the short name of Simple Mail Transfer Protocol, which is simply the message Transfer Protocol.

2. JavaMail

When we open an HTTP web link in Java code, we can usually use the HttpURLConnection class that has already been encapsulated in the HTTP protocol to quickly implement it. Java official also provides the Java class library which encapsulates the e-mail protocol, is javamail, but does not include in the standard JDK, needs us to go to Java or the Oracle official website to download.

:
Https://java.net/projects/javamail/pages/Home
Or
Http://www.oracle.com/technetwork/java/javamail/index.html

Here i download an up-to-date jar package from the Java.net website that contains the implementation of SMTP, IMAP, and POP3 protocols:

3. Create a simple email

First create a Java project, the download of the Javax.mail.jar as a class library to join the project, there is not much to say.

Message creation steps:

    1. Create a Mail object (mimemessage);
    2. Set sender, recipient, optional Add multiple recipients, cc person, secret send person;
    3. Set the subject (title) of the message;
    4. Set the body of the message (content);
    5. Set the sending time of the display;
    6. Save to Local.

Code implementation:

Package Com.xiets.javamaildemo;Import javax.mail.Session;Import javax.mail.internet.InternetAddress;Import Javax.mail.internet.MimeMessage;Import Java.io.FileOutputStream;Import Java.io.OutputStream;Import Java.util.Date;Import java.util.Properties;PublicClassMain {PublicStaticvoidMain (string[] args)Throws Exception {1. Create a message Properties props =New Properties ();parameter configuration for connecting to the mail server (only needed when sending mail) Session session= session.getdefaultinstance (props);Based on the parameter configuration, create a Session object (for sending mail) mimemessage message =New MimeMessage (session);Create a Mail object/* * can also create MimeMessage objects based on existing EML mail files * MimeMessage message = new MimeMessage (Session, New FileInputStream ("Myemail.eml")) ; */2. From: SenderThe three parameters of InternetAddress are: Mailbox, display nickname (only for display, no special requirement), nickname character Set encodingWhen you really want to send it, the mailbox must be a real, valid mailbox. Message.setfrom (New InternetAddress ("[Email protected]","User_aa","UTF-8"));3. To: Recipient Message.setrecipient (MimeMessage.RecipientType.TO,New InternetAddress ("[Email protected]","USER_CC","UTF-8"));To: Increase the recipient (optional) message.addrecipient (MimeMessage.RecipientType.TO,New InternetAddress ("[Email protected]","User_dd","UTF-8"));CC: CC (optional) message.setrecipient (MimeMessage.RecipientType.CC,New InternetAddress ("[Email protected]","User_ee","UTF-8"));BCC: Secret Send (optional) message.setrecipient (MimeMessage.RecipientType.BCC,new internetaddress ( "[email protected]", " User_ff ", " UTF-8 "); //4. Subject: Message subject Message.setsubject ( "test mail subject",  "UTF-8"); //5. Content: Message body (can use HTML tags) message.setcontent ( "Test this is the message body ... ", //6. Set the send time to display message.setsentdate (new Date ()); //7. Save the previous settings message.savechanges (); //8. Save the message to local outputstream out = new FileOutputStream ( "myemail.eml"); Message.writeto (out); Out.flush (); Out.close (); }}
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

The saved myemail.eml can be opened by using the mail client, which is actually a bunch of text that conforms to the SMTP protocol format (the content is encoded using base64) or Notepad, as shown here:


4. Send an email

Send mail first need to have an email account and password, this article to NetEase 163 mailbox For example, the mailbox account must be opened SMTP service, in the browser Web page login mailbox generally in the Mailbox "settings" option can be turned on, and note the SMTP server address of the mailbox, as shown below (the other mailbox is similar):

Code implementation:

Package Com.xiets.javamaildemo;Import javax.mail.Session;Import Javax.mail.Transport;Import javax.mail.internet.InternetAddress;Import Javax.mail.internet.MimeMessage;Import Java.util.Date;Import java.util.Properties;PublicClassMain {Sender's mailbox and password (replaced by their mailbox and password)Publicstatic String Myemailaccount ="[Email protected]";Publicstatic String Myemailpassword ="XXXXXX";The SMTP server address of the sender's mailbox must be accurate, different mail server addresses are different, the general format is: smtp.xxx.comNetEase 163 the SMTP server address of the mailbox is: smtp.163.comPublicstatic String Myemailsmtphost ="Smtp.163.com";Recipient mailbox (replace with a valid mailbox that you know)Publicstatic String Receivemailaccount ="[Email protected]";PublicStaticvoidMain (string[] args)Throws Exception {1. Create a parameter configuration for connecting to the mail server parameter configuration Properties props =New Properties ();Parameter Configuration Props.setproperty ("Mail.transport.protocol","SMTP");The protocol used (JavaMail specification requirements) Props.setproperty ("Mail.host", myemailsmtphost);The SMTP server address for the sender's mailbox Props.setproperty ("Mail.smtp.auth","true");Request authentication, parameter names are related to the specific implementation2. Create session objects based on configuration to interact with the mail server session session = Session.getdefaultinstance (props); Session.setdebug (true);Set to debug mode, you can view detailed send log3. Create a message MimeMessage message = Createmimemessage (Session, Myemailaccount, Receivemailaccount);4. Gets the message transfer object according to Session Transport Transport = Session.gettransport ();5. Use your email account and password to connect to your mail serverThe mailbox must be the same as the sender's mailbox in message, otherwise the error transport.connect (Myemailaccount, Myemailpassword);6. To send a message to all of the receiving addresses, message.getallrecipients () gets all the recipients that were added when the message object was created, cc person, bcc transport.sendmessage (message, Message.getallrecipients ());7. Close connection transport.close (); }/** * Create a simple message that contains only text * *Sessions that @param session and server interaction *@param sendMail Sender Email *@param receivemail Recipient Mailbox *@return *@throws Exception * *PublicStatic MimeMessageCreatemimemessage (Session session, String SendMail, String receivemail)Throws Exception {1. Create a message MimeMessage message =New MimeMessage (session);2. From: Sender Message.setfrom (New InternetAddress (SendMail," a treasure Net", "UTF-8")); //3. To: Recipients (can add multiple recipients, CC, BCC) message.setrecipient (MimeMessage.RecipientType.TO, new InternetAddress (Receivemail, " xx user", "UTF-8")); //4. Subject: Mail Subject Message.setsubject ("discount", "UTF-8"); //5. Content: Message body (can use HTML tags) message.setcontent ("xx user Hello, today full 50 percent, fast to buy, Miss today wait another year ... ", " Text/html;charset=utf-8 "); //6. Set the sender time Message.setsentdate (new Date ()); //7. Save Settings Message.savechanges (); return message;}}               
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21st
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

To view the recipient's Inbox after sending:



Create a complex message that contains text, pictures, and attachments see next: JavaMail-based Java mail delivery: Complex mail delivery

JavaMail-based Java Mail sending: Simple Mail sending

Related Article

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.