`
- 浏览:
674810 次
- 性别:
- 来自:
北京
-
java 发送邮件 例子 (带附件的) 使用org.apache.commons.mail
package mail;
import java.io.UnsupportedEncodingException;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
public class Send2 {
public Send2(){}
// 需要的jar包5个:commons-email-1.2.jar commons-email-1.2-javadoc.jar
// commons-email-1.2-sources.jar mail.jar activation.jar
public static void main(String[] args) {
send();
}
// QQ邮箱对应的pop3和smtp服务器地址为:pop3:pop.qq.com;
// smtp:smtp.qq.com,请参考
// lu@yahoo.com.cn
// 449222222@qq.com
public static void send() {
try {
MultiPartEmail email = new MultiPartEmail ();
email.setTLS(true);
email.setHostName("smtp.qq.com"); // 这里是发送服务器的名字
email.setAuthentication("449222222@qq.com", "邮箱密码");
//产生一个附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:/1.jpg");//指定附件在本地的绝对路径
//也可以以网络的方式
//attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("附件描述");//附件描述
//附件名称,如果附件是中文名会在乱码,attachment.setName(MimeUtility.encodeText("测试"));
attachment.setName(MimeUtility.encodeText("附件名字"));
//第2个参数是昵称,可以不要,收件人在信箱内的这个地方显示
email.setFrom("449020109@qq.com"); // 发送方
email.setSubject("Java发送邮件测试"); // 标题
email.setCharset("GB2312"); // 设置Charset
email.setMsg("这是一封Java程序发出的测试邮件,带附件。"); // 内容
email.attach(attachment);//添加附件
email.send();//发送
System.out.println("邮件发送成功");
} catch (EmailException e) {
e.printStackTrace();
}catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 有的邮箱需要开启POP3/SMTP服务,例如本例的qq邮箱,在测试时弹出需要开启此服务的 提示(qq邮箱发来的),进入邮箱的设置,开启就行了
*/
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet...
org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet...
org.apache.commons.commons-math3:3.6.1
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
jar包分享,你懂的 com.springsource.org.apache.commons.io-1.4.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.lang
com.springsource.org.apache.commons.logging-sources-1.1.1.jar
org.apache.commons.beanutils.jar
最新org.apache.commons.net..包完整、干净、实用
org.apache.commons.lang jar包
com.springsource.org.apache.commons.fileupload 自己打入本地Jar包即可
需要先 import org.apache.commons.lang3.StringUtils; /* 内含 common-lang3.jar commons-lang3-3.9-bin.zip commons-lang3-3.9-src.zip 使用说明.txt*/ public static String getClientIp(HttpServletRequest ...
org.apache.commons.lang所有包的集合,
com.springsource.org.apache.commons.pool-sources-1.5.3.jar源码
解决经常遇到的The import org.apache.commons.codec cannot be resolved问题,只需导入文件中的commons-codec-1.11.jar就可以,如和导入包就不赘述了。
org.apache.commons.httpclient
import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; ...
org.apache.commons.net.ftp.FTPClient包,不错,在实际项目和产品中使用过。
com.springsource.org.apache.commons.dbcp-sources-1.2.2.osgi.jar源码 jar包