JavaMail实现邮件发送的方法
程序员文章站
2024-03-04 13:01:05
本文实例讲述了javamail实现邮件发送的方法。分享给大家供大家参考。具体如下:
下载 activation.jar 和 mail.jar
配置classpath,将...
本文实例讲述了javamail实现邮件发送的方法。分享给大家供大家参考。具体如下:
下载 activation.jar 和 mail.jar
配置classpath,将上面两个jar包加入到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() { // 变量初始化 host = "smtp.qq.com"; username = ""; password = ""; fromaddr = "@"; } // 发送邮件 public boolean sendmail() { properties prop; session session; mimemessage msg; try { prop = new properties(); // 存储连接参数 prop.put("mail.smtp.host",host); prop.put("mail.smtp.auth","true"); session = session.getdefaultinstance(prop,null); // 获得一个邮件的session msg = new mimemessage(session); // 邮件信息 // 检查邮件地址是否合法 if(fromaddr == null || fromaddr.equals("")) { throw new exception("发件地址错误"); } if(toaddr == null || toaddr.equals("")) { throw new exception("目标地址错误"); } //设置源地址 msg.setfrom(new internetaddress(fromaddr)); //设置目的地址 msg.setrecipient(message.recipienttype.to,new internetaddress(toaddr)); //设置主题 msg.setsubject(subject); multipart mp = new mimemultipart(); //邮件内容 mimebodypart mbpcontent = new mimebodypart(); mbpcontent.setcontent(content,"text/html"); // 邮件格式 mp.addbodypart(mbpcontent); msg.setcontent(mp); msg.setsentdate(new date()); // 发送邮件 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"> <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>
希望本文所述对大家的java程序设计有所帮助。