Spring consolidation JavaMail Email with Gmail

Source: Internet
Author: User
Tags tomcat

After the spring consolidation JavaMail succeeded, a large amount of code was configured into the spring configuration file, reducing the JavaMail to mailboxes, ports, protocols, topics, content ... The coupling. You can send a message only if you need to invoke one line of code on the client.

First, you don't have to import jars into the project's lib, but you need to import 3 jar file Activation.jar in Tomcat's Lib, Jspsmartupload.jar,mail.jar import the 3 jar files so Tomcat doesn't have an error.

Sendmanagerservice. java

package com.cgt.itspiderman.service.email;

public interface SendManagerService {
    public void sendMail(String email);
}

Defines an interface for sending mail.

Sendmanagerserviceimp.java
Package com.cgt.itspiderman.service.email;

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

Import org.springframework.mail.SimpleMailMessage;
Import Org.springframework.mail.javamail.JavaMailSender;
Import Org.springframework.mail.javamail.MimeMessagePreparator;

public class Sendmanagerserviceimp implements Sendmanagerservice {

Private javamailsender Mailsende R

Private simplemailmessage message;

public void SendMail (final String email) {
//TODO auto-generated method stub

Mimeme Ssagepreparator preparator = new Mimemessagepreparator ()
{
public void prepare (MimeMessage Mim Emessage) throws Exception {

mimemessage.setrecipient (Message.RecipientType.TO, New internetaddr ESS (email));
Mimemessage.setfrom (New INternetaddress (Message.getfrom ()));
Mimemessage.setsubject (Message.getsubject (), "GBK");
Mimemessage.settext (Message.gettext (), "GBK");
}
};
Mailsender.send (preparator);    

}

Public Javamailsender Getmailsender () {
return mailsender;
   

public void Setmailsender (Javamailsender mailsender) {
This.mailsender = MailSender;
   

Public Simplemailmessage getMessage () {
return message;
   

public void Setmessage (Simplemailmessage message) {
This.message = message;
}

}

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.