当前位置:   article > 正文

java 发送mail

java 发送mail
package  zh;

import  java.util. * ;
import  javax.mail. * ;
import  javax.mail.internet. * ;

public   class  SendMainl  {
//smtp.sohu.com
    private static final String SMTP_HOST = "smtp.163.com";

    
private static final String SENDER_NAME = "熊猫飞天有限责任公司";

    
private static final String SENDER_EMAIL_ADDRESS = "mail@163.com";

    
public void sendConfirmation() {
        StringBuffer message 
= new StringBuffer();
        message.append(
"Hello!");
        message.append(
"哈哈!!");

        sendMessage(
"to@163.com""Sorry!java mail test", message.toString());
    }


    
protected void sendMessage(String recipient, String subject, String message) {
        Properties props 
= new Properties();
        props.put(
"mail.smtp.auth""true");
        props.put(
"mail.host", SMTP_HOST);
        props.put(
"mail.smtp.user""mail@163.com");
        props.put(
"mail.smtp.password""code");

        Session session 
= Session.getDefaultInstance(props, null);
        
try {
            Message msg 
= new MimeMessage(session);
            msg.setFrom(
new InternetAddress(SENDER_EMAIL_ADDRESS, SENDER_NAME));
            msg.setRecipient(Message.RecipientType.TO, 
new InternetAddress(
                    recipient));
            msg.setSubject(subject);
            msg.setSentDate(
new Date());
            msg.setText(message);
            Transport transport 
= session.getTransport("smtp");
            transport.connect((String) props.get(
"mail.smtp.host"), props
                    .getProperty(
"mail.smtp.user"), props
                    .getProperty(
"mail.smtp.password"));// 2
            transport.sendMessage(msg, msg.getAllRecipients());

        }
 catch (Exception e) {
            System.out.println(e);
        }


    }


    
public static void main(String[] args) {
        SendMainl sendMail 
= new SendMainl();
        sendMail.sendConfirmation();

    }

}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/591905
推荐阅读
相关标签
  

闽ICP备14008679号