javamail 指定发件人地址

现在的发件人地址是risingsuntest@hotmail.com我想用risingsuntest@hotmail.comrisingsuntest@hotmail.com进行登陆,而发件人的地址是yanlong137@hotmail.com ,大家有会的吗?

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

public class SendMail2 {

public static void sendMail(String toEmail, String title, String content) {
    String host = "smtp.live.com"; 
    String from = "wangjunlong2010@126.com"; 
    String to = toEmail; 
    final String username = "risingsuntest@hotmail.com"; 
    final String password = "skysnow"; 

    // 创建Properties 对象
    Properties props = System.getProperties();

    // 添加smtp服务器属性 添加了SSL验证
    props.put("mail.smtp.starttls.enable", "true");
    props.setProperty("mail.smtp.socketFactory.fallback", "true");
    // props.setProperty("mail.smtp.socketFactory.class",
    // "javax.net.ssl.SSLSocketFactory");

    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", username);
    props.put("mail.smtp.password", password);
    props.put("mail.smtp.port", "25"); // gmail smtp port 587
    props.put("mail.smtp.auth", "true");

    // 创建邮件会话
    Session session = Session.getDefaultInstance(props,
            new Authenticator() { // 验账账户
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {
        session.setDebug(true);
        // 定义邮件信息
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("yanlong137@hotmail.com"));// GB2312编码,防止接收后标题乱码
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));
        message.setSubject(title);
        message.setText(content);

        // 发送消息
        // session.getTransport("smtp").send(message); //也可以这样创建Transport对象
        Transport.send(message);

    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    SendMail2.sendMail("495924217@qq.com", "测试", "发送成功");

}

}

2个回答

[code="java"]

import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class JavaMailTest {

/**
 * @param args
 */
    private  String userName="yanlong137@hotmail.com";
   //下面是yanlong137@hotmail.com这个邮箱的密码
    private  String password ="********";
private  String host = "smtp.live.com";
private  String protocal = "smtp";
    private  String to = "risingsuntest@hotmail.com";

private static final Logger logger = Logger.getLogger(Object.class);

public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);
    message.setRecipients(RecipientType.TO, to);
    message.setFrom(new InternetAddress(userName));
    message.setSubject("测试邮件");
    message.setText("Hello World ","UTF-8");
    Transport transport = session.getTransport(protocal);
    transport.connect(host,userName,password);
    transport.sendMessage(message, message.getAllRecipients());
    System.out.println("--------send mail success-------------");
    logger.info("=============================");
}

}[/code]
yanlong137@hotmail.com是发送的邮箱,risingsuntest@hotmail.com是收件的邮箱

必须用risingsuntest@hotmail.com帐户登录hotmail邮箱,点"设置"-->"帐户",然后在“添加您拥有的其他电子邮件地址”里加入“yanlong137@hotmail.com”,最后在程序里指定发件人为yanlong137@hotmail.com(类似上面程序),这样收邮件的人看到的发件人才会是“yanlong137@hotmail.com”。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!