用 java怎么编写发邮件的程序 急急急急急急急 搜索一下,javamail。有的是现成的例子。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 搜索javamail就有很多相关资料了http://community.csdn.net/Expert/topic/3502/3502948.xml?temp=.1997492http://community.csdn.net/Expert/topic/3464/3464123.xml?temp=.9870417 import javax.mail.*;import javax.activation.*;import java.util.*;import javax.mail.internet.*;public class SendMail { public static void main(String[] args) { try { Properties props = new Properties(); Session sendMailSession; Store store; Transport transport; String host,user,pass,from,to;//都要赋值(主机地址,用户名,密码,寄信者,收信者) SmtpAuthenticator sa=new SmtpAuthenticator(user,pass); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth","true");// props.put("mail.smtp.user", user);// props.put("mail.smtp.password", pass);// sendMailSession = Session.getInstance(props, null); sendMailSession = Session.getInstance(props, sa); Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(from); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); newMessage.setSubject("测试发信"); newMessage.setSentDate(new Date()); newMessage.setText("测试发信成功,欢喜之中"); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); System.out.println ("发送成功"); } catch(Exception e) { e.printStackTrace(); } }} class SmtpAuthenticator extends Authenticator{ String username; String password; public SmtpAuthenticator(String s, String s1) { username = s; password = s1; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }} String address = "";String address_display = "no";HashMap sendadd_hashmap = new HashMap();sendadd_hashmap = this.sendAdd(request);address = (String) sendadd_hashmap.get("ADDRESS");if (address != null && address.length() > 0) { address_display = "yes";OperationBo opBo = new OperationBo(); String from = "[email protected]";String mail_subject =request.getParameter("subject");String mail_text = request.getParameter("message"); mail_subject = opBo.JPToUnicode(mail_subject);mail_text = opBo.JPToUnicode(mail_text);InternetAddress[] add = new InternetAddress[1];add[0] = new InternetAddress(address);Properties props = new Properties();props.put("mail.smtp.host", "mail.langchao.com");Session sess =Session.getDefaultInstance(props, null);try { message = new MimeMessage(sess); message.setFrom(new InternetAddress(from)); message.setRecipients( Message.RecipientType.TO, add); message.setSubject(mail_subject); message.setSentDate(new Date()); message.setText(mail_text); Transport.send(message); } catch (Exception e) { e.getMessage(); System.out.println(e.getMessage()); } JBPM3 token 'Token(/)'can't be signalled cause it is currently not positioned 关于hibernate中Load方法的一个问题,大家帮我瞧瞧 AJAX分页中如何获得JS里的变量值 求助!菜鸟Struts2问题 axis客户端小问题 关于JNative问题 问一个菜鸟中菜鸟得问题 高分求救(急),问题比较简单 struts中判断session失效的问题 spring boot 配置AOP时不生效 eclipse+Weblogic怎么去整合 请问Tomcat 5.0.29的配置问题,新手急等
http://community.csdn.net/Expert/topic/3464/3464123.xml?temp=.9870417
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;public class SendMail
{
public static void main(String[] args)
{
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
String host,user,pass,from,to;//都要赋值(主机地址,用户名,密码,寄信者,收信者)
SmtpAuthenticator sa=new SmtpAuthenticator(user,pass);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth","true");
// props.put("mail.smtp.user", user);
// props.put("mail.smtp.password", pass);
// sendMailSession = Session.getInstance(props, null);
sendMailSession = Session.getInstance(props, sa);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from);
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject("测试发信");
newMessage.setSentDate(new Date());
newMessage.setText("测试发信成功,欢喜之中");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
System.out.println ("发送成功");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class SmtpAuthenticator extends Authenticator
{ String username;
String password; public SmtpAuthenticator(String s, String s1)
{
username = s;
password = s1;
} protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}
String address_display = "no";
HashMap sendadd_hashmap = new HashMap();
sendadd_hashmap = this.sendAdd(request);
address = (String) sendadd_hashmap.get("ADDRESS");if (address != null && address.length() > 0) {
address_display = "yes";
OperationBo opBo = new OperationBo();
String from = "[email protected]";
String mail_subject =request.getParameter("subject");
String mail_text = request.getParameter("message");
mail_subject = opBo.JPToUnicode(mail_subject);
mail_text = opBo.JPToUnicode(mail_text);
InternetAddress[] add = new InternetAddress[1];
add[0] = new InternetAddress(address);Properties props = new Properties();
props.put("mail.smtp.host", "mail.langchao.com");Session sess =Session.getDefaultInstance(props, null);try {
message = new MimeMessage(sess);
message.setFrom(new InternetAddress(from));
message.setRecipients(
Message.RecipientType.TO,
add);
message.setSubject(mail_subject);
message.setSentDate(new Date());
message.setText(mail_text);
Transport.send(message);
} catch (Exception e) {
e.getMessage();
System.out.println(e.getMessage());
}