问题描述
现在的发件人地址是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 587props.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", "测试", "发送成功");}}
解决方案
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("=============================");}}yanlong137@hotmail.com是发送的邮箱,risingsuntest@hotmail.com是收件的邮箱
解决方案二:
必须用risingsuntest@hotmail.com帐户登录hotmail邮箱,点"设置"-->"帐户",然后在“添加您拥有的其他电子邮件地址”里加入“yanlong137@hotmail.com”,最后在程序里指定发件人为yanlong137@hotmail.com(类似上面程序),这样收邮件的人看到的发件人才会是“yanlong137@hotmail.com”。