JavaMail real-world mail sent using the Spring Framework package __JSONP

Source: Internet
Author: User
Tags ssl connection

Recently found that the use of their own POP3 server to send messages to users always have different types of mail received, such as my mail is the address is 123456@qq.com sent to 126 mailbox can be sent to QQ mailbox is not. After a half-day study and textual research finally determined the program using QQ POP3/SMTP mail server, the specific settings and configuration as follows:

1.JAVA Framework Spring Consolidated email mail Service Code configuration

<!--① Mail server-->

<bean id= "MailSender" class= "Org.springframework.mail.javamail.JavaMailSenderImpl" >

<property name= "Protocol" value= "SMTP"/>

<property name= "host" value= "smtp.qq.com"/>

<property name= "Port" value= "465"/><!--different mail providers offer different ports-->

<property name= "username" value= " 123456@qq.com "/>

<property name= "Password" value= "123456"/>

<property name= "Javamailproperties" >
<props>

<prop key= "Mail.smtp.auth" >true</prop>

<prop key= "Mail.smtp.starttls.enable" >true</prop>

<prop key= "Mail.smtp.socketFactory.class" >javax.net.ssl.SSLSocketFactory</prop>

<!--QQ Required SSL connection mail server to open SSL connection authentication -->

</props>

</property>

</bean>

Member Center, the user uses the mailbox to retrieve the password configuration

<!--Member Center action-->
<bean id= "web.member.IndexAction" class= "Com.tch.action.member.IndexAction" scope= "prototype" >
<property name= "Tchmemberservice" ref= "Tchmemberservice" ></property>
<property name= "MailSender" ref= "MailSender" ></property>
</bean>

2.JAVA Send mail class

import java.util.Date;

import javax.mail.internet.MimeMessage;

import Org.springframework.mail.javamail.JavaMailSender;

import Org.springframework.mail.javamail.MimeMessageHelper;

Public class Springmail {

/**

* Send HTML mail

* @param Sender Mail Sender entity

* @param toemail recipient's email address

* @param fromemail Sender's email address

* @param titleemail The title of the message sent

* @param contentemail The content of the message sent

* @throws Exception

*/

Public void sendhtmlmail (javamailsender sender,string toemail,string fromemail,string titleemail,string Contentemail)throws Exception {

MimeMessage msg = Sender.createmimemessage ();

Mimemessagehelper helper = new mimemessagehelper (msg,true, "UTF-8");

Mimemessagehelper helper = new Mimemessagehelper (msg);

Helper.setto (Toemail);

System.out.println (fromemail+ ">>" +toemail);

Helper.setfrom (Fromemail);

Helper.setsubject (Titleemail);

Helper.setsentdate (new Date ());

Helper.settext (Contentemail,true);

Sender.send (msg);

SYSTEM.OUT.PRINTLN ("Send mail successfully!");

}

}

3. Start the test, unsuccessful, the console error message:

Org.springframework.mail.MailAuthenticationException:Authentication failed; Nested exception is javax.mail.AuthenticationFailedException
[15:24:47.678] {http--8080-2$13326029} org.springframework.mail.MailAuthenticationException:Authentication failed ; Nested exception is javax.mail.AuthenticationFailedException

Permission validation failed.

Solution: (I suggest using QQ mailbox here)

If you encounter this exception, should think about your assigned to send a mailbox, there is no open pop3/smtp service, as far as I know, now NetEase has not opened up, QQ Mailbox has, but QQ mailbox in the default state, but also did not open this service, you have to "Mailbox Settings" account " Manually set up the POP3/SMTP service, the other mailbox server, only to try to say it.

4. Continuing the test is still unsuccessful and the error message is as follows:

501 Mail from address must is same as authorization user

Reason:

The main reason is the sending class Helper.setfrom (Fromemail);

Setfrom's mailbox account must be the same as the authentication server's mailbox account. For example, the above case must be helper.setfrom ("123456@qq.com");

WORKAROUND: Set the email address of the site to send the 123456@qq.com

5. Conclusion

It is important that the above configuration depends primarily on the services provided by the POP3/SMTP of the open platform.

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.