帮帮忙!现在要实现邮件发送(还必须要匿名发送)!
这是我写的代码:我用的是smtp.163.com 是不是163必须要经过验证用户名和密码的。我们经理看了说 最好用匿名发送(和我们做的东西有关)。高手帮我看下怎么解决!我也找了半天的资料,没实现!
public class SendMail {
public SendMail () {
}
private static String PROTOCOL = "smtp ";
private static String KEY_MAIL_SMTP_HOST = "mail.smtp.host ";
private static String KEY_MAIL_SMTP_PORT = "mail.smtp.port ";
private static String KEY_MAIL_SMTP_AUTH = "mail.smtp.auth ";
public static boolean send(String to , String from , String subject, String body, String affixFilename) {
String host = SystemConst.SMTP_HOST;//设置邮件服务器
String account = SystemConst.SMTP_USERNAME;//自己配置调用的用户名(默认的是我的)
String pwd = SystemConst.SMTP_PASSWORD;//密码
boolean br = false;
Session session;
Properties props = System.getProperties();
props.put(KEY_MAIL_SMTP_HOST, host);
props.put(KEY_MAIL_SMTP_AUTH, "true ");
MailAuthenticator ma = new MailAuthenticator (account, pwd);
session = Session.getInstance(props, null);
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
msg.setSubject(subject);
boolean blnAffixExist = false;
if (affixFilename == null || affixFilename.trim().length() == 0) {
blnAffixExist = false;
} else {
File file = new File(affixFilename);
if (file.exists()) {
blnAffixExist = true;
}
}
if (blnAffixExist == false) {
msg.setText(body);
} else {
Multipart mp = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(affixFilename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
BodyPart bp2 = new MimeBodyPart();
bp2.setContent(
" <meta http-equiv=Content-Type content=text/html; charset=gb2312> "
+ body, "text/html;charset=GB2312 ");
mp.addBodyPart(bp2);
msg.setContent(mp);
}
msg.setSentDate(new Date());
Transport transport = session.getTransport(PROTOCOL);
transport.connect((String) props.get(host),account, pwd);
transport.sendMessage(msg, msg.getAllRecipients());
//Transport.send(msg);
transport.close();
br = true;
} catch (Exception ex) {
ex.printStackTrace();
}
return br;
}
}
现在就是向不用通过验证用户名和密码(等于就是匿名