标题:
发送Mail时如何通过服务器的身份验证?(非JavaMail方式)
[打印本页]
作者:
yanzhizhouyu
时间:
2006-4-12 12:24
标题:
发送Mail时如何通过服务器的身份验证?(非JavaMail方式)
我写了一个发送mail的类,如下
import java.io.*;
import sun.net.smtp.SmtpClient;
public class Mail
{
public Mail()
{
}
public boolean sendMail(String from, String to, String subject, String content)
{
boolean result = false;
from="
xxxxx@163.com
";
to="
xxxxxxx@163.com
";
subject = "Sending email from Java!";
content = "Here write contents.";
try
{
SmtpClient client = new SmtpClient("smtp.163.com");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: " + subject);
message.println();
message.println("This was sent from a Java!");
message.println();
message.println("Cool class! :-)");
message.println();
message.println(content);
message.println();
client.closeServer();
result = true;
}
catch (IOException e)
{
System.out.println("ERROR SENDING EMAIL:"+e);
result = false;
}
finally
{
return result;
}
}
}
我在Win2k环境下试验,系统要求进行身份验证,请指教如何实现?希望写出源码。
作者:
微星
时间:
2006-4-12 12:25
需要提供你在163上面的用户名和密码
如果不需要这些的话,不是可以随便匿名发送垃圾邮件了么?
希望下面的代码有帮助
String message = "content";
Properties props = new Properties();
Session sendMailSession;
Transport transport;
sendMailSession = Session.getInstance(props, null);
sendMailSession.setDebug(true);
props.put("mail.smtp.host", "smtp.163.com");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("
xxxxx@163.com
"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("
xxxxx@163.com
"));
newMessage.setSubject("申请服务");
newMessage.setSentDate(new Date());
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(message);
MimeMultipart mmp = new MimeMultipart();
mmp.addBodyPart(mbp);
newMessage.setContent(mmp);
Transport.send(newMessage);
欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/)
Powered by Discuz! 6.0.0