注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
目的是用java mail来发送email,貌似我目前用的win7 home里面不能设置iis, anyway, 不太想在local设置smtp server.
下面是code,测试过,可以通过gmail发出email。msg.setFrom里面的gmail就是session里面用来登陆的。
我的问题是
1)能否继续使用gmail to create a session,但是把From设置成成其他的账号比如163/hotmail/other gmail account?这样收信方可以点击reply,直接reply to 163/hotmail/etc
2)如果上面方法不行,有其他的非gmail解决方案吗?
3)如果都不行,告诉我一个free/cracked local smtp server的软件吧。我用过1st mail server,不知道为什么,要过好久,信才会被发出,尽管我觉得我的设置应该是正确的。
=== java codes ============
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-gmail-account",
"your-gmail-password");
}
});
// ----------attach file
BodyPart filePart = new MimeBodyPart();
MimeMessage msg = new MimeMessage(session);
msg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse("gmail-to-receive-emails"));
msg.setSentDate(new Date());
Multipart mp = new MimeMultipart();
DataSource ds = new FileDataSource("path to your local file");
filePart.setDataHandler(new DataHandler(ds));
filePart.setHeader("Content-ID", "<logo>");
mp.addBodyPart(filePart);
// add the Multipart to the message
msg.setContent(mp);
// send the message
msg.setSubject("My Subject");
msg.setFrom(new InternetAddress("your-gmail"));
Transport.send(msg); |