手记

javamail发送邮件 发送代码参考

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import junit.framework.TestCase;

public class JMail extends TestCase {
    public void testName() throws AddressException, 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 msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(""));
        msg.setText("this is the first mail by java programing!");
        msg.setSubject("SendMessageDemo1");
        Transport transport = session.getTransport();
        transport.connect("smtp.sina.com", 25, "", "");
        transport.sendMessage(msg, new Address[] { new InternetAddress("") });
        transport.close();
    }
}
1人推荐
随时随地看视频
慕课网APP