java邮件客户端源代码,基于MyEclipse/NetBeans开发

可以话,大神可以给完整的工程文件... 可以话,大神可以给完整的工程文件 展开
 我来答
yu46612143
2014-07-03 · TA获得超过105个赞
知道小有建树答主
回答量:172
采纳率:100%
帮助的人:113万
展开全部
package unibase.ee.javamail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

public class JavaMailSender {

private static final Logger logger = Logger.getLogger(JavaMailSender.class);

public void send(String host,String user,String password,String from,String to,String subject,String content,String filename) throws JavaMailSendException {
String[] filenames = new String[1];
filenames[0] = filename;
send(host,user,password,from,to,subject,content,filenames);
}

/**
*
* @param host 用于发送的邮箱提供商的制式
* @param user 用于发送的邮箱用户名
* @param password 用于发送的邮箱密码
* @param from 用于发送的邮箱地址
* @param to 接收端邮箱地址
* @param subject 标题
* @param content 内容
* @param filenames 附件
* @throws JavaMailSendException
*/
public void send(String host,String user,String password,String from,String to,String subject,String content,String[] filenames) throws JavaMailSendException {

Properties props = new Properties();
props.put("mail.smtp.host", host);
// props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

try {
MyAuthenticator auth = new MyAuthenticator(user, password);
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(props,(Authenticator) auth);
MimeMessage mmessage = new MimeMessage(sendMailSession);
//设置收、发地址
mmessage.setFrom(new InternetAddress(from));
mmessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
//设置标题
mmessage.setSubject(subject,"UTF-8");
Multipart multipart = new MimeMultipart();
//设置正文
MimeBodyPart mbpText=new MimeBodyPart();
mbpText.setText(content, "UTF-8");
multipart.addBodyPart(mbpText);
//设置附件
for(String filename : filenames) {
MimeBodyPart mbpFile=new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
int index1 = filename.lastIndexOf("/");
int index2 = filename.lastIndexOf("\\");
String appendName = filename;
if(index1 > 0) {
appendName = filename.substring(index1 + 1);
} else if(index2 > 0) {
appendName = filename.substring(index2 + 1);
}
mbpFile.setFileName(appendName);
multipart.addBodyPart(mbpFile);
}
mmessage.setContent(multipart);
//设置发送时间
mmessage.setSentDate(new java.util.Date());
//发送邮件
//Transport.send(mmessage);
Transport transport = sendMailSession.getTransport("smtp");//指定的协议
transport.connect(host,user,password);
transport.sendMessage(mmessage,mmessage.getAllRecipients());
transport.close();
} catch(Throwable e) {
logger.error("send mail fail : ",e);
throw new JavaMailSendException((Exception)e);
}
System.out.println("发送邮件成功!");
}

}

class MyAuthenticator extends Authenticator {
private String username;

private String userpass;

public MyAuthenticator(String un, String up) {
super();
username = un;
userpass = up;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, userpass);
}
}
追问
这是基于什么平台的?
追答
这只是代码。我是用eclipse
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式