欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javaMail入门实例

程序员文章站 2022-06-01 13:27:29
...

package August;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* 必须导入smtp.jar包,如果导入包后发生异常(找不到原因)
* 尝试将MyEclipse中自带的mail相关的包删除
* @author snaillocke
*
*/
public class JavaMail {
public static void main(String[] args) throws MessagingException {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol","smtp");
Session session =Session.getInstance(props);
session.setDebug(true);

Message message = new MimeMessage(session);
message.setText("发件内容");
message.setFrom(new InternetAddress("发件邮箱地址"));
Transport transport = session.getTransport();
transport.connect("smtp.xx.com",25,"发件邮箱用户名","发件邮箱密码");
transport.sendMessage(message, new Address[]{new InternetAddress("收件邮箱地址")});
transport.close();
}

}

相关标签: Myeclipse