javamail 发邮件问题求助

pgy112 2012-12-31 05:14:07
如果需要输入mail服务器的用户名和密码,请问下怎么加啊,之前不输入的源码如下(可以发邮件
import sun.net.smtp.*;
import sun.net.*;
import java.io.*;
public class sendmail { // a java smtp client
public static void main (String args[ ]) {
File myFile;
String s;
String subject= new String(args[2]);
String from=new String(args[3]);
String to=new String(args[0]);
try{
myFile = new File(args[1]);
RandomAccessFile fis= new RandomAccessFile(myFile,"r");
SmtpClient client = new SmtpClient("172.0.0.5");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: "+subject+"\n");
while((s = fis.readLine()) != null)
{
s=s+"\n";
message.println(s);
}
client.closeServer();
fis.close();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
}
}
...全文
122 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
猪公公 2013-01-09
  • 打赏
  • 举报
回复
public static void main(String[] args) throws InterruptedException { Properties props=new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); Session session=Session.getInstance(props); session.setDebug(true); Message msg=new MimeMessage(session); Transport transport=null; try { Address to = new InternetAddress("收件邮箱地址@.com"); msg.setRecipient(Message.RecipientType.TO,to); transport=session.getTransport(); msg.setContent("<html><table border='2'><tr><td>姓名:</td><td>屌丝小马</td></tr>" + "<tr><td>酒吧欠债:</td><td>一千万</td></tr>" + "</table></html>", "text/html;charset=gbk"); msg.setFrom(new InternetAddress("发件邮箱地址@.cn")); msg.setSubject("欠债通知"); transport.connect("smtp.sina.com", 25, "发件邮箱地址@.cn", "发件邮箱密码"); transport.sendMessage(msg, new Address[]{new InternetAddress("收件邮箱地址@.com")}); transport.close(); } catch (MessagingException e) { e.printStackTrace(); } }

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧