Implement code to send mail using Java _java

Source: Internet
Author: User

Objective

Please note: The following code to use the test mailbox for 126 of the mailbox, measured available, attention, personal testing, pay attention to send the message content, mail content Mailbox Server review more stringent, rarely inadvertently will be judged as spam, and then you will return an error code:

Instance Code

Import Java.util.Date;

Import java.util.Properties;
Import Javax.mail.Authenticator;
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;

Import javax.mail.internet.MimeUtility; public class Sendmailutil {private static final string USERNAME = "username";//sender's username private static final string passwo RD = "password"; Send mail person's password private static final String HOSTNAME = "smtp.126.com"; SMTP server address private static final String fromaddress = USERNAME + "@126.com"; Sender Mailbox private static final String fromusername = "cncd-Code on China Blog"; Sender name, can write private static final String Emailtitle = "Code on China Blog"; Message headers private static final String emailcontent = "Welcome to the cncd-Code on China blog Activation Mail, please click on the right link to complete account activation: \ n";/message content public static void Main (string[] args) {try {SendEmail ("825338623@qq.com "," User name: Guopengfei, Password: Guopengfei ");
 catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); /** * Send mail * @param sendaddress email address * @param content sent * @throws Exception/public static void Sen
 Demail (string sendaddress, String content) throws Exception {//Create a connection property. Properties Props = new properties (); Props.put ("Mail.smtp.host", HOSTNAME); The server address for setting up SMTP is Smtp.126.com props.put ("Mail.smtp.auth", "true");
 Set the SMTP server to authenticate.
 Props.put ("Mail.transport.protocol", "stmp"); Session session = Session.getinstance (props, New authenticator () {protected passwordauthentication Getpasswordauthenti
  cation () {Return to New Passwordauthentication (USERNAME, PASSWORD);
 }
 });
 Message message = new MimeMessage (session); InternetAddress from = new InternetAddress (fromaddress); Sender Address From.setpersonal (Mimeutility.encodetext (fromusername));
 Sender name Message.setfrom (from); InternetAddress to = new InternetAddress ("TaOzhida11@sina.cn "); Recipient address//Create message body: message.setrecipients (Message.RecipientType.TO, Internetaddress.parse (sendaddress));//Copy to more than one person's mail Box//Message.setrecipient (Message.RecipientType.TO, to); Only CC to own mailbox Message.setsubject (Mimeutility.encodetext (emailtitle));
 Message header message.setsentdate (new Date ());
 Mimemultipart Msgmultipart = new Mimemultipart ("mixed");//specified as mixed relationship message.setcontent (Msgmultipart);
 Message content MimeBodyPart Htmlpart = new MimeBodyPart (); Htmlpart.setcontent ("<body><div style= ' width:1000px;height:300px;margin:0px auto;margin-bottom:20px; border:1px solid #92B0DD; Background-color: #FFFFFf; ' > 

Note that the first use of this Code friends, recommended in addition to the user name and password for their own, the rest of the best not to change, otherwise it is easy to be found in NetEase mailbox spam and not to send, generally determined as spam, NetEase returned the error code as follows:

com.sun.mail.smtp.smtpsendfailedexception:554 DT:SPM 126 smtp2,dmmowacxotzmk8vxvdknea--. 1778S2 1472566222,please http://mail.163.com/help/help_spam_16.htm?ip=117.114.147.13&hostid=smtp2&time=1472566222 at

 Com.sun.mail.smtp.SMTPTransport.issueSendCommand (smtptransport.java:1829) at
 Com.sun.mail.smtp.SMTPTransport.finishData (smtptransport.java:1634) at
 Com.sun.mail.smtp.SMTPTransport.sendMessage (smtptransport.java:889) at
 Sendmailutil.sendemail ( sendmailutil.java:88) at
 Sendmailutil.main (sendmailutil.java:29)

It means that you go to the link in the error return code to view the Help information, you can go to the link and find that the 554 error code above represents the meaning. This is generally the case for spam.

Second, the test does not frequently run (send) the above code in the mail, too frequent, but also easy to determine as spam. Well, the above is the entire content of this article, I hope that the study and work can help.

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.