java邮件开发代码 java开发邮件发送功能

求java实现邮件发送的源代码import java.util.*;
import javax.mail.*;import javax.mail.internet.*;
public class JMail {
public void SendMail(String Topic,String Content){Properties props=new Properties();props.put("mail.smtp.host","smtp.163.com");props.put("mail.smtp.auth","true");Session s=Session.getInstance(props);s.setDebug(false);MimeMessage message=new MimeMessage(s);MimeMultipart mp=new MimeMultipart();BodyPart body = new MimeBodyPart();InternetAddress from;InternetAddress to;try{from=new InternetAddress("发件人邮箱");message.setFrom(from);to = new InternetAddress("收件人邮箱");message.setRecipient(Message.RecipientType.TO,to);message.setSubject(Topic,"utf-8");body.setContent(Content, "text/html;charset=utf-8");mp.addBodyPart(body);message.setContent(mp);message.setSentDate(new Date());message.saveChanges();Transport transport=s.getTransport("smtp");transport.connect("smtp.163.com(邮件服务商java邮件开发代码,这是163java邮件开发代码的)","发件邮箱","发件邮箱密码");transport.sendMessage(message,message.getAllRecipients());transport.close();}catch(AddressException e){e.printStackTrace();}catch(MessagingException e){e.printStackTrace();}}}
java编写小型的局域网邮件发送我给你提供一个我在项目里面实际使用的代码.
这是我基于一个网上的代码自己修改封装过来的.
你可以参考一下
/**
*
* @author Sglee
*
*/
public class SimpleMail {
private static String encode = null;
static {
if ("\\".equals(File.separator)) {
encode = "GBK";
} else {
encode = "UTF-8";
}
}
/**
* 以文本格式发送邮件
*
* @param mailInfo
* @return
*/
public static boolean sendTextMail(MailInfo mailInfo) {
for (int i = 0; i3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证java邮件开发代码,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// mailMessage.setText(mailInfo.getContent());//设置邮件消息的主要内容
// MimeMultipart类是一个容器类java邮件开发代码,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含附件内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);

推荐阅读