Java:Java发送邮件简单例子

Java:Java发送邮件简单例子

import java.util.Properties;


import javax.mail.Message;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;


public class MailTransfor {

public static void main(String[] args) throws Exception {

Properties props = new Properties();

// 要使用用户名,密码进行认证登录,如果是直接给smtp服务器发就不用这个了

props.setProperty("mail.smtp.auth", "true"); 

props.setProperty("mail.transport.protocol", "smtp");

Session session = Session.getInstance(props);

session.setDebug(true);


String str = "是我做得不好";

Message message = new MimeMessage(session);

message.setText(str);

message.setSubject("对不起");

message.setFrom(new InternetAddress("[email protected]", "黄彪"));


Transport transport = session.getTransport(); // transport的一些信息保存在session

transport.connect("smtp.qq.com", 25, "26664141", "password");

transport.sendMessage(message, InternetAddress.parse("[email protected]"));

transport.close();

}

}


你可能感兴趣的:(Java:Java发送邮件简单例子)