篇首语:逆风的方向,更适合飞翔。本文由小编为大家整理,主要介绍了知识大全 使用JavaMail 实现邮件发送相关的知识,希望对你有一定的参考价值。
使用JavaMail 实现邮件发送 以下文字资料是由(全榜网网)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
import java io *; import java util *; import javax mail *; import javax mail internet *; import javax activation *;
public class SendMail private String from; private String to; private String username; private String password; private String subject; private String text; private File file; public SendMail() public SendMail(String from String to String username String password) this from=from; this to=to; this username=username; this password=password; public String getFrom() return from; public void setFrom(String from) this from = from; public String getTo() return to; public void setTo(String to) this to = to; public String getUsername() return username; public void setUsername(String username) this username = username; public String getPassword() return password; public void setPassword(String password) this password = password; public String getSubject() return subject; public void setSubject(String subject) this subject = subject; public String getText() return text; public void setText(String text) this text = text; public File getFile() return file; public void setFile(File file) this file = file; public boolean send() Properties props=new Properties(); props put( mail smtp host ); //props put( mail smtp host localhost ); props put( mail smtp auth true); Session mailSession=Session getDefaultInstance(props null); try Transport trans=mailSession getTransport( smtp ); nnect( username password); // nnect(); Message newMessage=new MimeMessage(mailSession); newMessage setSubject(subject); newMessage setFrom(new InternetAddress(from)); /* * 上传一个文件 * */ BodyPart fileBodyPart=new MimeBodyPart(); FileDataSource fds=new FileDataSource(file); fileBodyPart setDataHandler(new DataHandler(fds)); fileBodyPart setFileName( a wav ); Address addressTo[] = new InternetAddress( ); newMessage setRecipients(Message RecipientType TO addressTo); newMessage setText(text); /* * 将文件保存到Message中 * */ MimeMultipart multi = new MimeMultipart(); multi addBodyPart(fileBodyPart); newMessage setContent(multi);
lishixinzhi/Article/program/Java/hx/201311/26077