Java Programs Send mail

Source: Internet
Author: User
Tags mail account ssl connection

Before the internet has seen others summed up the use of Java programs to send mail, so they come down to practice, to learn some of their experiences summed up.

First of all, we need to use two jar packages here:

Need friends can go online to csdn a similar website to find

By the way, the test case is pasted out, there are some detailed comments, the next will be reminded to write a demo of the time we have some attention to the place. Put in the middle of your own problems. Posted for your reference.

1. First ensure that the sender's mailbox is open with the SMTP protocol of the recipient's mailbox. ------"can be opened in the mailbox settings.

The use of QQ mailbox here we can find in the mailbox help QQ mailbox POP3 with the SMTP server address has opened the corresponding port is generally 465, or 587

2. Extremely important : Now many mailboxes are using authorization code to open POP3 and SMTP services to third parties, so we use the mailbox account password to log in to the mailbox, remember to change the password to the appropriate authorization code .

(Need our mobile phone to send SMS verification)

3. We test the contents of the sent message may be blocked by the mail server as spam, we need to set up here. Add it to a whitelist of messages to avoid anti-spam errors

4. For sending failed error, we can carefully analyze the error code code, the Internet to find the corresponding error codes information, to find the cause of the error.

Here for the sending mailbox is NetEase:

http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html

The test demo is as follows:

Package Test;

Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.util.Date;
Import java.util.Properties;

Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;

public class Testmail {
e-mail Account
public static String Ownemailaccount = "[email protected]";
//Email password------"Authorization Code
public static String Ownemailpassword = "xxxxxxxxxxx";
//SMTP server address for sending mail
public static String Myemailsmtphost = "smtp.163.com";
Send mail to each other's mailbox
public static String Receivemailaccount = "[email protected]";

public static void Main (string[] args) throws Exception {
Properties prop = new properties ();
To set the protocol SMTP used for message transport
Prop.setproperty ("Mail.transport.protocol", "SMTP");
Set the SMTP address of the sender's mail server
Here to NetEase's mailbox SMTP server address as an example
Prop.setproperty ("Mail.smtp.host", myemailsmtphost);
Setting the validation mechanism
Prop.setproperty ("Mail.smtp.auth", "true");

The port of the SMTP server (the port of the non-SSL connection is typically 25 by default and can be added without the SSL connection enabled.
You need to change the port of the SMTP server that corresponds to the mailbox to see Help for the corresponding mailbox service.
QQ Mailbox SMTP (SLL) port is 465 or 587, other mailboxes to see for themselves)

/*final String smtpport = "465";
Prop.setproperty ("Mail.smtp.port", Smtpport);
Prop.setproperty ("Mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Prop.setproperty ("Mail.smtp.socketFactory.fallback", "false");
Prop.setproperty ("Mail.smtp.socketFactory.port", smtpport); * *

Create object reply to interact with server
Session session = Session.getinstance (prop);
Session with Debug mode
Session.setdebug (TRUE);
Create a Mail object
Message message = Createsimplemail (session);

Transport trans = Session.gettransport ();
Linked mail server
Trans.connect (Ownemailaccount, Ownemailpassword);
Send Message
Trans.sendmessage (Message, message.getallrecipients ());
Close link
Trans.close ();
}

/**
* @Title: Createsimplemail
* @Description: Create a Mail object
* @author: Chengpeng
* @param @param session
* @param @return
* @param @throws Exception settings file
* @return Message return type
* @throws
*/
public static MessageCreatesimplemail(Session session) throws Exception {
MimeMessage message = new MimeMessage (session);
Set the sending email address, param1 on behalf of the sending address param2 the name sent by the representative (any) param3 the name encoding method
Message.setfrom (New InternetAddress ("[Email protected]", "Zhang San", "Utf-8"));
On behalf of the recipient
Message.setrecipient (Message.RecipientType.TO, New internetaddress (Receivemailaccount, "John Doe", "Utf-8"));
To: Add recipients (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")); /
Set Message subject
Message.setsubject ("Test forward mail");
Set up message content
Message.setcontent ("Good morning, world how are you lately!") "," Text/html;charset=utf-8 ");
Set Send Time
Message.setsentdate (New Date ());
Save the edits above
Message.savechanges ();
Writes the object created above to the local
OutputStream out = new FileOutputStream ("myemail.eml");
Message.writeto (out);
Out.flush ();
Out.close ();
return message;

}

}

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

Java Programs Send mail

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.