JavaMail implements the method of mail delivery
- 2020-04-01 03:48:36
- OfStack
This article illustrates an example of how JavaMail implements mail delivery. Share with you for your reference. The details are as follows:
Download the activation.jar and mail.jar
Configure the CLASSPATH and add the above two jars to the CLASSPATH.
Javabeans: "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 E-mail
public boolean sendMail()
{
Properties prop;
Session session;
MimeMessage msg;
try
{
prop = new Properties(); //Memory connection parameter
prop.put("mail.smtp.host",host);
prop.put("mail.smtp.auth","true");
session = Session.getDefaultInstance(prop,null);
//Get an email Session
msg = new MimeMessage(session); //Email messages
//Check that the email address is valid
if(fromAddr == null || fromAddr.equals(""))
{
throw new Exception(" Sending address error ");
}
if(toAddr == null || toAddr.equals(""))
{
throw new Exception(" Target address error ");
}
//Set the source address
msg.setFrom(new InternetAddress(fromAddr));
//Set destination address
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddr));
//Set the theme
msg.setSubject(subject);
Multipart mp = new MimeMultipart(); //Email content
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setContent(content,"text/html"); //Mail format
mp.addBodyPart(mbpContent);
msg.setContent(mp);
msg.setSentDate(new Date());
//Send E-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;
}
}
The 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">
<html>
<head>
<title>Mail</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<center>
<jsp:useBean id="email" class="cls.SendEmail" scope="page" />
<jsp:setProperty name="email" property="toAddr" value="host" />
<jsp:setProperty name="email" property="fromAddr" value="host" />
<jsp:setProperty name="email" property="username" value="username" />
<jsp:setProperty name="email" property="password" value="password" />
<jsp:setProperty name="email" property="subject" value="mail test" />
<jsp:setProperty name="email" property="content" value="hello world"/>
<%=email.sendMail() %>
</center>
</body>
</html>
I hope this article has been helpful to your Java programming.