java mail 回复邮件_javamail 回复邮件代码

展开全部

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);

}

}*/

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值