Simple implementation JMail Send mail

Source: Internet
Author: User

Package Com.chauvet.util;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import Org.apache.log4j.Logger;

/**
* for sending JMail Mail
* need to use Smtpauthenticator.java
* @author WXW
 *
 */
Public class JMail {
private String hostsmtp = "smtp.126.com";//Mailbox SMTP
private String hostaddress = "*******@126.com";//Outbox Address
private String hostpwd = "pwd";//Outbox Password
private static Logger log = Logger.getlogger (Jmail.class.getName ());

    /**
* Send JMail
     *
* @param title
* Email title
* @param content
* Email Content
* @param toaddress
* Receive email address such as: [email protected]
     */
Public void SendMail (string title, string content, String toaddress) {
try {
String mail = content;
//properties contains the address of the sending mail server
Properties Mailprops = new properties ();
mailprops.put ("Mail.smtp.host", hostsmtp);
mailprops.put ("Mail.smtp.auth", "true");
smtpauthenticator smtpauthenticator = new Smtpauthenticator (hostaddress, hostpwd);
Session mailsession = session.getdefaultinstance (mailprops,smtpauthenticator);
mimemessage message = new MimeMessage (mailsession);
Message.setfrom (New InternetAddress (hostaddress));
message.setrecipient (Message.RecipientType.TO, New InternetAddress (Toaddress, false));
Message.setsubject (title);
message.settext (mail);
transport.send (message);
} catch (Exception exc) {
log.error ("Send Mail" +title+ "Exception!") ");
exc.printstacktrace ();
        }
    }

Public static void Main (string[] args) {
jmail AA = new JMail ();
Aa.sendmail ("title", "Content", "[email protected]");
System.out.println ("Well done!");
    }
}

Simple implementation JMail Send mail

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.