`
竹临仙
  • 浏览: 37860 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

tools----java---->mail

阅读更多
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;
}
}
分享到:
评论

相关推荐

    spring+struts+hibernate+dwr+jstl做的实例

    2.0.rc2.jar&lt;br&gt;jaxen-full.jar&lt;br&gt;jaxp-api.jar&lt;br&gt;jdbc2_0-stdext.jar&lt;br&gt;jstl.jar&lt;br&gt;mail.jar&lt;br&gt;mysql-connector-java-5.0.4-bin.jar&lt;br&gt;sax.jar&lt;br&gt;saxpath.jar&lt;br&gt;standard.jar&lt;br&gt;xalan.jar&lt;br&gt;xercesImpl....

    JAX-WS 2.2 RI所有相关jar包

    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....

    James mail server&client installation&configuration

    -- If this is set to a non-local email address, the mail server --&gt; &lt;!-- will still function, but will generate a warning on startup. --&gt; &lt;postmaster&gt;Postmaster@iss-sd3-svr9&lt;/postmaster&gt; &lt;!-- ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    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 ...

    新版Android开发教程.rar

    � 暂不具备 Push Mail 和 Office(DataViz 、 QuickOffice 计划近期推出 ) 功能,目前主要面向的是普通消费 者 用户,对商业用户支持尚弱。 Android Android Android Android 带来的影响 ANDROID 的推出后可能影响的...

    activation.jar和mail.jar和tools.jar

    在Java开发领域,尤其是涉及到Web服务和服务器配置时,`activation.jar`、`mail.jar`和`tools.jar`是三个非常关键的库文件。在本篇文章中,我们将深入探讨这三个库文件的作用,以及它们如何在Tomcat服务器和Axis Web...

    AXIS2实现webservice jar包

    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

    标题 "axis1.4+activation.jar+mail.jar" 指涉的是一个基于Java的Web服务开发工具包,Axis1.4,以及两个必要的库文件:activation.jar和mail.jar。这些组件在Java应用程序中,尤其是涉及到网络通信和邮件处理时扮演...

    OSDK Tools-开源

    OSDK (Open Service Development Kit) 工具是一套针对开发者设计的开源软件,它提供了对多种网络协议和服务的简化Java接口,包括FTP(文件传输协议)、XML(可扩展标记语言)处理、Mail(电子邮件)、HTTP(超文本...

    centOs7安装Android Sdk详细教程

    在开发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

    Java邮件开发Fundamentals of the JavaMail API

    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

    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模板引擎工具包

    ant-javamail-1.6.jar.zip

    &lt;taskdef name="mail" classname="org.apache.tools.ant.taskdefs.optional.mail.Mail"&gt; &lt;classpath&gt; &lt;pathelement location="path/to/ant-javamail-1.6.jar"/&gt; &lt;/classpath&gt; &lt;/taskdef&gt; ``` 然后就可以在构建...

    基于SpringBoot的图书馆管理系统项目源码+数据库+项目说明(课程设计).zip

    spring-boot-starter-mail&lt;br&gt; &emsp;&emsp;&emsp;&emsp;spring-boot-starter-thymeleaf&lt;br&gt; &emsp;&emsp;&emsp;&emsp;spring-boot-starter-web&lt;br&gt; &emsp;&emsp;&emsp;&emsp;spring-boot-starter-test&lt;br&gt; org....

    基于SpringBoot的图书馆管理系统源码+数据库+项目说明(毕设).zip

    spring-boot-starter-mail&lt;br&gt; &emsp;&emsp;&emsp;&emsp;spring-boot-starter-thymeleaf&lt;br&gt; &emsp;&emsp;&emsp;&emsp;spring-boot-starter-web&lt;br&gt; &emsp;&emsp;&emsp;&emsp;spring-boot-starter-test&lt;br&gt; org....

    jax-ws-2.2.rar

    在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环境变量配置

    - `%JAVA_HOME%\tools.jar` 包含了一些Java开发工具类库。 通过这样的配置,当运行Java程序时,JVM会自动加载这些指定路径下的类文件和库文件,从而确保程序的正常运行。 ### 总结 综上所述,正确配置`JAVA_HOME`...

Global site tag (gtag.js) - Google Analytics