java實現郵件的發送分享方法如下
public class PopupAuthenticator extends Authenticator{
public PasswordAuthentication getPasswordAuthentication()
{
String username=
String pwd =
return new PasswordAuthentication(username
}
}
public class SendMail {
public static void main(String[] args) {
try {
Authenticator auth = new PopupAuthenticator()
Properties mailProps = new Properties()
//郵件信息驗證
mailProps
mailProps
mailProps
mailProps
Session mailSession = Session
MimeMessage message = new MimeMessage(mailSession)
message
Address toInternetAddress=new InternetAddress(
message
message
message
MimeMultipart multi = new MimeMultipart()
BodyPart textBodyPart = new MimeBodyPart()
//textBodyPart
multi
message
message
//下面代碼是發送附件
String fileName =
MimeBodyPart messageBodyPart = new MimeBodyPart()
messageBodyPart
Multipart multipart = new MimeMultipart()
multipart
messageBodyPart = new MimeBodyPart()
DataSource source = new FileDataSource(fileName)
messageBodyPart
messageBodyPart
multipart
message
//推送郵件和附件信息
Transport
System
} catch (Exception ex) {
System
System
ex
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27084.html