发邮件时终于可以通过sina的smtp验证了(附代码)

80酷酷网    80kuku.com

  sina|smtp|发邮件import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;


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

        String host = "smtp.sina.com.cn";
        String from =  "javamailsina.com";
        String to = "javamailchina.com";
        String username = "javamail";
        String password = "password";

        // Get system properties
        // Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
        Properties props = new Properties();

        // Setup mail server
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true"); //这样才能通过验证

        // Get session
        Session session = Session.getDefaultInstance(props);

        // watch the mail commands go by to the mail server
        session.setDebug(true);

        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO,
          new InternetAddress(to));
        message.setSubject("Hello JavaMail");
        message.setText("Welcome to JavaMail");

        // Send message
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: