展开全部
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
Title: java mail
*
Description:
*
Copyright: Copyright (c) 2004
*
Company:
* @author petehero
* @version 1.0
*/
class SendMail
{
/**
* Logger for this class
*/
private static String server = "smtp.163.com";//smtp主机名。
private static String from = "XXXXXX@163.com";//发送方邮件地址
private static String pw = "XXXXXX";//发送方邮件密码。
private static String to = "XXXXXX@163.com"; //接收方邮件地址
static String subject="hello";
/*BodyPart bp = new MimeBodyPart();
Multipart mp = new MimeMultipart();
DataSource source = new FileDataSource("a.jpg");//附件
*/
static String mailContent="你好!\n\n"+"\t邮件正文32313133353236313431303231363533e78988e69d8331333236383434test\n\n";//邮件正文
public static void send()
{
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",server);
props.put("mail.smtp.user",from);
props.put("mail.smtp.password",pw);
//PopupAuthenticator popA=new PopupAuthenticator();
//PasswordAuthentication pop = popA.performCheck(from, pw);
//sendMailSession = Session.getInstance(props,popA);//填写用户名及密码
sendMailSession = Session.getInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication() //邮件安全认证。
{
return new PasswordAuthentication(from, pw);
}
});
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
/*
bp.setText("bptext");
mp.addBodyPart(bp);
bp = new MimeBodyPart();
bp.setDataHandler(new DataHandler(source));
bp.setFileName("141.txt");
mp.addBodyPart(bp);
newMessage.setContent(mp);
*/
mailContent+=new Date().toLocaleString();
newMessage.setText(mailContent);
transport = sendMailSession.getTransport("smtp");
//System.out.println("Sending...");
//transport
Transport.send(newMessage);
//Transport.send(newMessage );
//System.out.println(server+from+pw+to);
}
catch (MessagingException ex)
{
}
}
// public static void main(String[] args)
// {
//
for(int i=0;i
{
System.out.println(args[i]);
}
//
// from=args[0].trim();
// pw=args[1].trim();
// SendMail sml = new SendMail();
// sml.send();
String a=SendMail.mailSend("hello","world");
System.out.println(a);
// }
static String mailSend(String t_to,String t_subject,String t_text)
{ String state="";
to=t_to;
subject=t_subject;
mailContent=t_text+"\n\n";
SendMail sml = new SendMail();
sml.send();
state="Successful";
return state;
}
/* public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}*/
}
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起