- 浏览: 37860 次
- 性别:
- 来自: 重庆
最新评论
package cn.java;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail {
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = false; // smtp是否需要认证
private String username = ""; // smtp认证用户名和密码
private String password = "";
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
public SendMail() {
// setSmtpHost(getConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取
createMimeMessage();
}
public SendMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
/**
* @param hostName
* String
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
}
/**
* @return boolean
*/
public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
}
catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
}
/**
* @param need
* boolean
*/
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
/**
* @param name
* String
* @param pass
* String
*/
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
/**
* @param mailSubject
* String
* @return boolean
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
/**
* @param mailBody
* String
*/
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
}
/**
* Just do it as this
*/
public static void main(String[] args) {
// 发送的邮件内容
String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ "<div align=center><h1 color=red >java,测试</h1></div>";
// 指明让那个smtp转发(搜狐)
SendMail themail = new SendMail("smtp.163.com");
// 校验身份
themail.setNeedAuth(true);
// 邮件标题
if (themail.setSubject("java发邮件") == false)
return;
// 将要发送的 内容放入邮件体
if (themail.setBody(mailbody) == false)
return;
// 发送到哪里
if (themail.setTo("收到") == false)
return;
// 谁发送的
if (themail.setFrom("发送") == false)///??????
return;
//附件
if(themail.addFileAffix("D:\\图片\\搞笑\\rt.jpg") == false) return;
// if()
themail.setNamePass("发送", "密码");//???
// 发送
if (themail.sendout() == false)
return;
}
}
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail {
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = false; // smtp是否需要认证
private String username = ""; // smtp认证用户名和密码
private String password = "";
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
public SendMail() {
// setSmtpHost(getConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取
createMimeMessage();
}
public SendMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
/**
* @param hostName
* String
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
}
/**
* @return boolean
*/
public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
}
catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
}
/**
* @param need
* boolean
*/
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
/**
* @param name
* String
* @param pass
* String
*/
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
/**
* @param mailSubject
* String
* @return boolean
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
/**
* @param mailBody
* String
*/
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
}
/**
* Just do it as this
*/
public static void main(String[] args) {
// 发送的邮件内容
String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ "<div align=center><h1 color=red >java,测试</h1></div>";
// 指明让那个smtp转发(搜狐)
SendMail themail = new SendMail("smtp.163.com");
// 校验身份
themail.setNeedAuth(true);
// 邮件标题
if (themail.setSubject("java发邮件") == false)
return;
// 将要发送的 内容放入邮件体
if (themail.setBody(mailbody) == false)
return;
// 发送到哪里
if (themail.setTo("收到") == false)
return;
// 谁发送的
if (themail.setFrom("发送") == false)///??????
return;
//附件
if(themail.addFileAffix("D:\\图片\\搞笑\\rt.jpg") == false) return;
// if()
themail.setNamePass("发送", "密码");//???
// 发送
if (themail.sendout() == false)
return;
}
}
发表评论
-
对象的字段以键值对的形式返回
2011-10-11 21:22 1997但是,如果双向关联都设置成fetch = FetchType. ... -
得到本机的ip地址
2011-06-25 13:12 1149public static String getL ... -
怎么获得Map<String,Date>中 String或Date类型
2011-06-16 09:28 4771有一个要求就是获得范型中类型;想了很多招都不能实现。 但有框架 ... -
javascript检验xml是否正确
2011-01-04 20:07 1041<script type="text/java ... -
图片防止盗链 转转kaka100
2011-01-03 16:11 648转。。转。。 -
新发现----享元模式
2011-01-02 23:54 718java1.5新知识: public class A { ... -
得到汉字的拼音
2011-01-02 15:17 933package cn.java; public class ... -
设置图片等比例缩小
2011-01-02 15:04 792//----------------------------- ... -
网页中一些特殊字符的转换,如[image]
2011-01-02 14:51 983package com.email.util; public ... -
BigDecimal 的学习
2010-12-31 00:23 725package com.util; import java. ... -
人民币
2010-12-31 00:09 811package com.util; public class ... -
单例模式 转转转
2010-12-30 19:49 751单例模式的七种写法 文章分类:Java编程 转载请注明出处: ... -
文件压缩
2010-12-30 13:17 663package com.email.util; import ... -
Cookie的一些操作
2010-12-30 13:15 673package com.email.util; import ... -
servlet处理参数的一些操作
2010-12-30 13:09 603import javax.servlet.http.HttpS ... -
字符串与时间格式的相互操作
2010-12-30 13:06 831import java.text.ParseException ... -
文件的相关操作 转转转
2010-12-30 13:02 753package com.Utils; import java ... -
oracle 连接... 修改.....查询
2010-12-30 12:49 605package com.Utils; import java. ... -
工具类-------字符串转成时间格式
2010-12-20 20:14 652package cn.java; import java.t ... -
处理中文乱码(新,比较万能)(encodeURI)
2010-12-19 01:04 1132$.ajax({ type:"GET ...
相关推荐
2.0.rc2.jar<br>jaxen-full.jar<br>jaxp-api.jar<br>jdbc2_0-stdext.jar<br>jstl.jar<br>mail.jar<br>mysql-connector-java-5.0.4-bin.jar<br>sax.jar<br>saxpath.jar<br>standard.jar<br>xalan.jar<br>xercesImpl....
javax.mail_1.4.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jaxws-api.jar jaxws-rt.jar jaxws-rt-javadoc.jar jaxws-tools.jar jaxws-tools-javadoc.jar jsr181-api.jar management-api.jar mimepull....
-- If this is set to a non-local email address, the mail server --> <!-- will still function, but will generate a warning on startup. --> <postmaster>Postmaster@iss-sd3-svr9</postmaster> <!-- ...
Using the Command-Line Tools 22 Using an Integrated Development Environment 25 Running a Graphical Application 28 Building and Running Applets 31 Chapter 3: Fundamental Programming Structures ...
� 暂不具备 Push Mail 和 Office(DataViz 、 QuickOffice 计划近期推出 ) 功能,目前主要面向的是普通消费 者 用户,对商业用户支持尚弱。 Android Android Android Android 带来的影响 ANDROID 的推出后可能影响的...
在Java开发领域,尤其是涉及到Web服务和服务器配置时,`activation.jar`、`mail.jar`和`tools.jar`是三个非常关键的库文件。在本篇文章中,我们将深入探讨这三个库文件的作用,以及它们如何在Tomcat服务器和Axis Web...
mail-1.4.jar mex-1.7.4-impl.jar neethi-3.0.3.jar stax2-api-3.1.1.jar tribes-6.0.16.jar woden-core-1.0M10.jar woodstox-core-asl-4.2.0.jar wsdl4j-1.6.2.jar xalan-2.7.0.jar xml-resolver-1.2.jar xmlbeans-...
标题 "axis1.4+activation.jar+mail.jar" 指涉的是一个基于Java的Web服务开发工具包,Axis1.4,以及两个必要的库文件:activation.jar和mail.jar。这些组件在Java应用程序中,尤其是涉及到网络通信和邮件处理时扮演...
OSDK (Open Service Development Kit) 工具是一套针对开发者设计的开源软件,它提供了对多种网络协议和服务的简化Java接口,包括FTP(文件传输协议)、XML(可扩展标记语言)处理、Mail(电子邮件)、HTTP(超文本...
在开发vue时想将vue转换为apk安装包 先卸载掉自带的jdk、推荐使用1.8的jdk cd /usr/local/src/ mkdir sdkMy ...export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL export PATH=$PATH:/u
mail-enabled through the JavaMail API. Concepts After completing this module you will understand the: * Basics of the Internet mail protocols SMTP, POP3, IMAP, and MIME * Architecture of the ...
OSChina 项目中用到的所有的jar包 1 activation.jar 邮件需要的处理内容的包 2 c3p0-0.9.1.2.jar C3P0连接池 3 commons-beanutils-1.8.2.jar Bean工具包 ...26 velocity-tools-1.4.jar velocity模板引擎工具包
<taskdef name="mail" classname="org.apache.tools.ant.taskdefs.optional.mail.Mail"> <classpath> <pathelement location="path/to/ant-javamail-1.6.jar"/> </classpath> </taskdef> ``` 然后就可以在构建...
spring-boot-starter-mail<br>     spring-boot-starter-thymeleaf<br>     spring-boot-starter-web<br>     spring-boot-starter-test<br> org....
spring-boot-starter-mail<br>     spring-boot-starter-thymeleaf<br>     spring-boot-starter-web<br>     spring-boot-starter-test<br> org....
在Tomcat上发布webservice所需要的jar包,总共25个:FastInfoset.jar/gmbal-api-only.jar/ha-api.jar/javax.annotation.jar/javax.mail_1.4.jar/jaxb-api.jar/jaxb-impl.jar/jaxb-xjc.jar/jaxws-api.jar/jaxws-rt....
- `%JAVA_HOME%\tools.jar` 包含了一些Java开发工具类库。 通过这样的配置,当运行Java程序时,JVM会自动加载这些指定路径下的类文件和库文件,从而确保程序的正常运行。 ### 总结 综上所述,正确配置`JAVA_HOME`...