首页 > 代码库 > java发送邮件
java发送邮件
import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.MimeMessage;public class SendMail { static Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username@163.com", "pwd");//发件人的用户名和密码 } }; public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.163.com");// props.put("mail.smtp.auth", "true"); props.put("mail.from", "username@163.com"); Session session = Session.getInstance(props, auth); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, "收件人邮箱"); msg.setSubject("标题"); msg.setSentDate(new Date()); msg.setText("我这里是内容........."); Transport.send(msg); } catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); } }}
ps:需要导入mail.jar包
http://download.csdn.net/download/qq531783564/7548771
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。