扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
import java.io.IOException; yC_.cBhm
import java.io.PrintWriter; Dm;tW"-
import java.util.Properties; [qBp~ -
import javax.mail.*; dX2 "]tV&
import javax.mail.internet.*; e3R.3F
import javax.naming.Context; U&Q(L~wH
import javax.naming.InitialContext; h]8$Ni0
import javax.activation.*; j!x=y! -w
dgbJ^/iX
6,M Tr4
public class SendMailTest extends Object U5wyt"r
{ 8 w!9L
public static class SMTPAuthenticator extends Authenticator $ziXYmO
{ :dg`=i57
private String username; Qr4uvT-,
private String password; Mr[/4d6:=R
%P; zGLQr
public SMTPAuthenticator(String username, String password) A +RsH -
{ 0~j)Tp
super(); $g2#+42_
this.username = username; i[atEq/
this.password = password; mJ$mvU
} t#\p* 10
tAugd }v
protected PasswordAuthentication getPasswordAuthentication() GOl>rz=
{ BNOIs4E
return new PasswordAuthentication(this.username, this.password); k/0JCV(v
} (BF!gjm>m>
} T!Wq[qlsvV
\-8Dg?1i%
public static void main (String argv[]) qlG[|,NXXB
{ hBlmy1
try { gxz2^@meZ)
//Get system properties 2GB[1>za/
Properties props = System.getProperties(); R}-a{# E
L`I
//Specify the desired SMTP server &pR$ >z<
props.put("mail.smtp.host", "smtp.sina.com.cn");//SMTP服务器 %5 VjN
props.put("mail.smtp.auth", "true");//发送邮件时是否需要验证身份 xYhBG~
props.put("mail.mime.charset", "GB2312");//邮件正文的字符集 YA0nB#5h
JcC)XU<
String username = "yourname"; //邮箱用户名 'v.!;I0(L
String password = "yourpassword";//邮箱密码 FBjPk:
'AjS +~
SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password); //用户身份验证 C+}PR0uIry
` *K"H4L\'
// create a new Session object Lf!UDk&$
Session session = Session.getInstance(props, authenticator); Gd0zZC,
~7nhSf+*
session.setDebug(true); //是否打印调试信息的标记 %~`o1#l%
Ug%3,
String from = "yourname@sina.com"; //发送方邮箱 Nx3bJ *~q
String to = "touser@domain"; //接收方邮箱 Y_V!bs_
String subject = "\"this is_a_测?试.\""; //邮件标题 cfaS(ilc
String body = "hi\n 邮件内容 body!!!"; //邮件正文 e]'T`c
`vzk'
// create a new MimeMessage object (using the Session created above) eUb7\9rZ
MimeMessage message = new MimeMessage(session); B^*<\W?
message.setFrom(new InternetAddress(from, "发送方姓名", "gb2312")); S/nf~RkG-
InternetAddress[] address = new InternetAddress[] { new InternetAddress(to, "接收人姓名", "gb2312") }; 2J?$HI!W~
f ?_Y!E)H8
message.setRecipients(Message.RecipientType.TO, address); pZRLyHv \
message.setSubject(subject, "GB2312"); ~KGxEYJ9|
)a ykfa(e
//如果不需要带附件,直接使用以下两行代码即可 _x)pYRGx
//==start of 独立正文发送 s iVc
//message.setContent(body, "text/plain"); u$q3c:W C
//message.setText(body, "GB2312"); Fsznd-..
//==end of 独立正文发送 :BMZ*b1k
2C&v%- M
uf5RJQ
//====start of 带附件发送 FDT(a;Qr
MimeBodyPart mbp1 = new MimeBodyPart(); bX:kZ`!+v
mbp1.setText(body, "GB2312"); //设置邮件正文 ClaEwj6$Wz
0i ;lq=+[
//以下设置附件 Zg-{toFK
// create the second message part n f;?eFq
MimeBodyPart mbp2 = new MimeBodyPart(); 4A.Hlnm
// attach the file to the message t/*G$,t;
FileDataSource fds= ztIEm<~
new FileDataSource("c:\\test.wml"); cr$/\G$T`
mbp2.setDataHandler(new DataHandler(fds)); l sM vo
mbp2.setFileName("test.wml"); 3N%
c,4*e*s|X
// create the Multipart y73LN+M
//and its parts to it uRt5hVo}x
Multipart mp = new MimeMultipart(); <>'!$Dn
mp.addBodyPart(mbp1); s08y*F
mp.addBodyPart(mbp2); }hX));1f0:
// add the Multipart to the message .B/MOji
message.setContent(mp); "8n' ";oJ
//====end of 带附件发送 iq5ad>R
0.!b4if
Transport.send(message); RWODj~
\cG|T)X
// it worked! $d xDzJI
System.out.println("Thank you. Your message to " + to + " was successfully sent."); W5;MI81$W
w.dQ8P":
} catch (Throwable t) { #ZR7+OU7
nHsyax
System.out.println("Unable to send message:"); aI< +
t.printStackTrace(System.out); #z8 K0
} ayQF^4vO
} KUnzunQb
} S}}|@P
5(\]uL#
查看本文来源
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者