JavaMail method to implement mail delivery _java

Source: Internet
Author: User

The example of this article describes how JavaMail implements mail delivery. Share to everyone for your reference. Specifically as follows:

Download Activation.jar and Mail.jar
Configure Classpath to add the top two jar packs to the classpath.

JavaBean: "Sendemail.java"

Package CLS; 
Import Java.util.Date; 
Import java.util.Properties; 
Import javax.mail.*; 
Import javax.mail.internet.*; 
 public class SendEmail {String host; 
 String username; 
 String password; 
 String fromaddr; 
 String toaddr; 
 String subject; 
 String content; 
 Public SendEmail () {//variable initialization host = ' smtp.qq.com '; 
 Username = ""; 
 Password = ""; 
 FROMADDR = "@"; 
 ///Send mail public boolean sendMail () {Properties prop; 
 Session session; 
 MimeMessage msg; 
  try {prop = new Properties ();///Storage Connection Parameters Prop.put ("Mail.smtp.host", host); 
  Prop.put ("Mail.smtp.auth", "true");
  Session = Session.getdefaultinstance (prop,null); Get the session msg = new mimemessage for a message (session); 
  Mail Message/Check email address is valid if (fromaddr = null | | fromaddr.equals ("")) {throw new Exception ("Send address Error"); 
  } if (toaddr = null | | toaddr.equals ("")) {throw new Exception ("Destination address error"); 
  //Set Source address Msg.setfrom (new internetaddress (FROMADDR)); Set Destination Address Msg.setRecipient (Message.recipienttype.to,new internetaddress (toaddr)); 
  Set subject Msg.setsubject (subject); Multipart MP = new Mimemultipart (); 
  Message content MimeBodyPart mbpcontent = new MimeBodyPart (); Mbpcontent.setcontent (Content, "text/html"); 
  Message Format Mp.addbodypart (mbpcontent); 
  Msg.setcontent (MP); 
  Msg.setsentdate (New Date ()); 
  Send mail Transport transport = Session.gettransport ("SMTP"); 
  Transport.connect ((String) prop.get ("Mail.smtp.host"), Username,password); 
  Transport.sendmessage (Msg,msg.getrecipients (MimeMessage.RecipientType.TO)); 
  Transport.close (); 
 return true; 
  catch (Exception e) {System.out.println (e); 
 return false; 
 }//getter and setter public String GetPassword () {return password; 
 } public void SetPassword (String password) {this.password = password; 
 Public String GetHost () {return host; 
 public void Sethost (String host) {this.host = host; Public String GetUserName () {return username; 
 } public void Setusername (String username) {this.username = username; 
 Public String getfromaddr () {return this.fromaddr; 
 } public void Setfromaddr (String addr) {fromaddr = addr; 
 Public String gettoaddr () {return this.toaddr; 
 } public void Settoaddr (String addr) {toaddr = addr; 
 Public String Getsubject () {return subject; 
 public void Setsubject (String sub) {subject = sub; 
 Public String getcontent () {return content; 
 public void SetContent (String content) {this.content = content; } 
}

Sendmail.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

I hope this article will help you with your Java programming.

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.