Java based on JavaMail implementation to send mail to QQ email _java

Source: Internet
Author: User
Tags auth

Recently, the project is doing news crawler, want to achieve this function: Crawler a page failure, the URL of the page to send to the mailbox. The final implementation of the results are as follows, the latter can be added filter labels, failure status codes, and so on to facilitate classification search anomalies.

Developers can analyze the cause of the crawler failure based on the URL and stack information in the message.

    • Is the server down?
    • Or is the crawler's Dom parsing not resolved to the content?
    • Or is the regular expression not available for this page?

Turn on the SMTP service

Open SMTP Service in-> account in QQ mailbox

Note that after the opening, QQ mailbox will generate an authorization code, in the code to connect the mailbox use this authorization code rather than the original mailbox password, so you can avoid using plaintext password.


On the Internet for an example, according to this article Java Mail (ii): JavaMail Introduction and send a simple message sample code.

 properties props = new properties ();
Turn on debug Debug Props.setproperty ("Mail.debug", "true");
The sending server requires authentication Props.setproperty ("Mail.smtp.auth", "true");
Set the mail server host name Props.setproperty ("Mail.host", "smtp.qq.com");

Send mail protocol name Props.setproperty ("Mail.transport.protocol", "SMTP");

Session session = Session.getinstance (props);
Message Content Part msg = new MimeMessage (session);
Msg.setsubject ("Seenews error");
StringBuilder builder = new StringBuilder ();
Builder.append ("url =" + "http://blog.csdn.net/never_cxb/article/details/50524571");
Builder.append ("page crawler error");
Builder.append ("\ n data" + timetool.getcurrenttime ());
Msg.settext (Builder.tostring ());

Mail Sender Msg.setfrom ("* * sender's email address * *"));
Send mail Transport transport = Session.gettransport ();

Transport.connect ("smtp.qq.com", "* * Email Address * * *," * * * your email password or authorization code * *);
Transport.sendmessage (MSG, new address[] {new InternetAddress ("* * Recipient's email address * *")});

Transport.close (); 

But it's an error.

Debug Smtp:auth Login Command trace suppressed
debug Smtp:auth login failed
Exception in thread "main" Javax.mai l.authenticationfailedexception:530 
error:a Secure connection is requiered (such as SSL). More information at http://service.mail.qq.com/cgi-bin/help?id=28

Because the example code is used 163 mailbox, and the author is QQ mailbox, see LOG analysis is QQ mailbox need SSL encryption.

Turn on SSL encryption

Online search a bit, others such as 163, Sina Mailbox does not require SSL encryption, you can give up QQ mailbox.

On the internet there is a kind of saying, the smtp.qq.com replaced by smtp.exmail.qq.com also do not require SSL encryption, but the author did not run successfully. So let's just add SSL encryption.

The following code opens the SSL encryption

Mailsslsocketfactory SF = new Mailsslsocketfactory ();
Sf.settrustallhosts (true);
Props.put ("Mail.smtp.ssl.enable", "true");
Props.put ("Mail.smtp.ssl.socketFactory", SF);

Successfully, the console output Log and the effect diagram are as follows

Debug Smtp:useehlo True, Useauth true
debug smtp:trying to connect to host "smtp.qq.com", Port 465, Isssl true
2 smtp.qq.com Esmtp QQ Mail Server
DEBUG smtp:connected to Host "smtp.qq.com", port:465
...
 Data 2016-01-19 17:00:44 Tue
.
ok:queued as 
QUIT
221 Bye

Complete code example

public class Mailtool {public static void main (string[] args) throws Messagingexception, Generalsecurityexception {

    Properties Props = new properties ();
    Turn on debug Debug Props.setproperty ("Mail.debug", "true");
    The sending server requires authentication Props.setproperty ("Mail.smtp.auth", "true");
    Set the mail server host name Props.setproperty ("Mail.host", "smtp.qq.com");

    Send mail protocol name Props.setproperty ("Mail.transport.protocol", "SMTP");
    Mailsslsocketfactory SF = new Mailsslsocketfactory ();
    Sf.settrustallhosts (TRUE);
    Props.put ("Mail.smtp.ssl.enable", "true");

    Props.put ("Mail.smtp.ssl.socketFactory", SF);

    Session session = Session.getinstance (props);
    msg = new MimeMessage (session);
    Msg.setsubject ("Seenews error");
    StringBuilder builder = new StringBuilder ();
    Builder.append ("url =" + "http://blog.csdn.net/never_cxb/article/details/50524571");
    Builder.append ("\ n page crawler error");
    Builder.append ("\ n Time" + timetool.getcurrenttime ()); Msg.settext (BuilDer.tostring ());

    Msg.setfrom (New InternetAddress ("* * sender's email address * *)");
    Transport transport = Session.gettransport ();

    Transport.connect ("smtp.qq.com", "* * Email Address * * *," * * * your email password or authorization code * *);
    Transport.sendmessage (MSG, new address[] {new InternetAddress ("* * Recipient's email address * *")});
  Transport.close ();
 }
}

The above is the entire content of this article, I hope to help you learn.

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.