Sending of Java and Spring Mail

Source: Internet
Author: User

Method One:

Java send e-mail: Here to send QQ Mail as an example:

Package test;

Import java.util.Properties;

Import Javax.mail.Authenticator;
Import Javax.mail.BodyPart;
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;

public class Mysendemail {
public static void Main (string[] args) {
try {
String userName = "Sender qq number @qq.com";
String Password = "Sender's mailbox password";
String smtp_server = "smtp.qq.com"; QQ Mailbox mail server address
String from_mail_address = userName;
String to_mail_address = "The receiver QQ number @qq.com";
Authenticator auth = new Popupauthenticator (userName, password);
Properties Mailprops = new properties ();
Mailprops.put ("Mail.smtp.host", smtp_server);
Mailprops.put ("Mail.smtp.auth", "true");
Mailprops.put ("username", username);
Mailprops.put ("password", password);

Session mailsession = session.getdefaultinstance (Mailprops, auth);
Mailsession.setdebug (TRUE);
MimeMessage message = new MimeMessage (mailsession);
Message.setfrom (New InternetAddress (from_mail_address));
Message.setrecipient (Message.RecipientType.TO, New internetaddress (
to_mail_address));
Message.setsubject ("Mail testw");

Mimemultipart multi = new Mimemultipart ();
BodyPart Textbodypart = new MimeBodyPart ();
Textbodypart.settext ("Java e-mail test content w");
Textbodypart.setfilename ("37af4739a11fc9d6b311c712.jpg");
Textbodypart.setfilename ("E:\\api\\w3c.chm");

Multi.addbodypart (Textbodypart);
Message.setcontent (multi);
Message.savechanges ();
Transport.send (message);
} catch (Exception ex) {
System.err.println ("The reason the message failed to send is:" + ex.getmessage ());
SYSTEM.ERR.PRINTLN ("Specific cause of error");
Ex.printstacktrace (System.err);
}
}
}

Class Popupauthenticator extends Authenticator {
Private String username;
private String password;

Public Popupauthenticator (string username, string pwd) {
This.username = Username;
This.password = pwd;
}

Public Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (This.username, This.password);
}
——————————————————————————————————————————————————————------————————————————————————

Method Two: Use spring to provide the mail function, also to send QQ mailbox as an example:

  Beans.xml File Code:

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!--Note: The parameters here (such as user name, password) are for the sender of the message--
<bean id= "MailSender" class= "Org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name= "Host" >
<value>smtp.qq.com</value>
</property>
<property name= "Javamailproperties" >
<props>
<prop key= "Mail.smtp.auth" >true</prop>
<prop key= "Mail.smtp.timeout" >25000</prop>
</props>
</property>
<property name= "username" >
<value> Sender QQ No. @qq.com</value>
</property>
<property name= "Password" >
<value> Sender QQ Email password </value>
</property>

</bean>
</beans>

Java code to send the message:

Package test;

Import Java.util.UUID;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.mail.SimpleMailMessage;
Import Org.springframework.mail.javamail.JavaMailSender;

public class SendMail {

Public ApplicationContext CTX = null;
Public SendMail () {
Get context
CTX = new Classpathxmlapplicationcontext ("Beans.xml");
}
public void Send () {
Get Javamailsender Bean
Javamailsender sender = (javamailsender) ctx.getbean ("MailSender");
Simplemailmessage mail = new Simplemailmessage (); <span style= "COLOR: #ff0000" > Note Simplemailmessage can only be used to send messages in text format </SPAN>


try {
Mail.setto ("Recipient @qq.com");//Recipient
Mail.setfrom ("sender @qq.com");//sender, here can also be another email alias, not with the username in the XML consistent
Mail.setsubject ("Spring Mail test!"); /Theme
Mail.settext ("Springmail simple send test, with random verification code:" +uuid.randomuuid (). toString (). substring (0, 6));//message content

Sender.send (mail);
} catch (Exception e) {
E.printstacktrace ();
}
}

public static void Main (string[] args) {
for (int i=0;i<100;i++) {

SendMail SendMail = new SendMail ();
Sendmail.send ();
SYSTEM.OUT.PRINTLN ("Send complete ...");

}

}
}

Complete....

Sending of Java and Spring Mail

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.