关于Java中的JavaMail问题
给你个例子吧
/**
* @author EX-QINCIDONG001
* 验证
*/
public class MailAuthorization extends Authenticator {
private String username;
private String password;
public MailAuthorization(String username,String password) {
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username,password);
}
}
public class MailInfo {
// 服务器地址
private String hostName;
// 端口
private String hostPort;
// 用户名
private String userName;
private String password;
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
// 是否需要验证
private boolean validate = false;
// 发件人
private String sender;
// 收件人
private String[] recvers = {};
// 抄送给哪些人
private String[] ccs = {};
// 邮件主题
private String subject;
// 邮件内容
private String content;
// 附件
private ListFile attaches;
public String getHostName() {
return this.hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getHostPort() {
return this.hostPort;
}
public void setHostPort(String hostPort) {
this.hostPort = hostPort;
}
public boolean isValidate() {
return this.validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSender() {
return this.sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String[] getRecvers() {
return this.recvers;
}
public void setRecvers(String[] recvers) {
this.recvers = recvers;
}
public String[] getCcs() {
return this.ccs;
}
public void setCcs(String[] ccs) {
this.ccs = ccs;
}
public String getSubject() {
return this.subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
public ListFile getAttaches() {
return this.attaches;
}
public void setAttaches(ListFile attaches) {
this.attaches = attaches;
}
}
public class MailSender {
private MailInfo mailInfo;
public MailSender(MailInfo mailInfo) {
this.mailInfo = mailInfo;
}
// 发送邮件方法
public void sendMail() throws MessagingException {
Properties props = System.getProperties();
props.setProperty("mail.transport.protocol", "smtp"); // smtp协议
props.put("mail.smtp.host", mailInfo.getHostName());
props.put("mail.smtp.port", mailInfo.getHostPort());
props.put("mail.smtp.auth", mailInfo.isValidate()?"true":"false");
Authenticator authenticator = new MailAuthorization(mailInfo.getUserName(),mailInfo.getPassword());
Session session = Session.getDefaultInstance(props, authenticator);
MimeMessage msg = new MimeMessage(session);
msg.setSubject(mailInfo.getSubject(), "utf-8");
Address address = new InternetAddress(mailInfo.getSender());
msg.setFrom(address);
// 收信人
Address[] recvs = new Address[mailInfo.getRecvers().length];
int index = 0;
if (mailInfo.getRecvers() != null) {
for (String s : mailInfo.getRecvers()) {
Address addr = new InternetAddress(s);
recvs[index] = addr;
index++;
}
}
msg.setRecipients(RecipientType.TO,recvs);
index = 0;
// 抄送
Address[] ccs = new Address[mailInfo.getCcs().length];
if (mailInfo.getCcs() != null) {
for (String s:mailInfo.getCcs()) {
Address addr = new InternetAddress(s);
ccs[index] = addr;
index++;
}
}
msg.setRecipients(RecipientType.CC, ccs);
Multipart mp = new MimeMultipart();
BodyPart part = new MimeBodyPart();
part.setContent(mailInfo.getContent(), "text/html;charset=utf-8");
// 增加附件
if (mailInfo.getAttaches() != null mailInfo.getAttaches().size() 0) { // 有附件
for (int i=0;imailInfo.getAttaches().size();i++) {
BodyPart bp = new MimeBodyPart();
FileDataSource fds = new FileDataSource((File) mailInfo.getAttaches().get(i));
bp.setDataHandler(new DataHandler(fds));
mp.addBodyPart(bp);
}
}
mp.addBodyPart(part);
msg.setContent(mp);
// Transport transport = session.getTransport();
Transport.send(msg);
}
}
public class TestMailSender {
/**
* @param args
*/
public static void main(String[] args) {
MailInfo mi = new MailInfo();
mi.setValidate(true);
mi.setUserName("ex-qincidong001@pingan.com.cn");
mi.setPassword("*******");
mi.setSubject("测试邮件发送");
mi.setSender("qincidong@163.com");
mi.setRecvers(new String[]{"ex-qincidong001@pingan.com.cn","qincidong@qq.com"});
mi.setHostPort("25");
mi.setHostName("smtp.163.com");
mi.setContent("a href=''百度/a百度知道");
// 添加附件
ListFile attaches = new ArrayListFile();
attaches.add(new File("D:\\qincidong\\资料\\Ejb\\EJB3.0入门经典-weblogic.pdf"));
attaches.add(new File("D:\\qincidong\\资料\\Ejb\\编写第一个EJB应用程序.doc"));
mi.setAttaches(attaches);
MailSender sender = new MailSender(mi);
try {
sender.sendMail();
System.out.println("邮件发送成功。");
} catch (MessagingException e) {
System.err.println("邮件发送失败");
e.printStackTrace();
}
}
}
java 定义一个"邮箱"用什么类型
java 定义一个"邮箱"必然用String类型
String 不是定义汉字类型的时候用的么?不是的。所有东西基本都可以用string来定义,所以string用的是最普遍的。
这时你会想问,那我所有东西都用string定义不就可以了么?为什么还要搞那么 多类型?
看下面
int,long等数字类型方便计算,比如1+1=2方便计算
boolean类型只有两个值,方便判断true和false,等等。
所以当不是日期,不是数字的些东西基本上都是用string业定义的
关于javamail发送邮件的回执
如果是指检验邮箱是否存在的话,没必要去发送邮件,当然如果发送邮件的话也是可以检验出来的,如果邮箱不存在的话,发送是失败的;更简单一点直接用javaMail中的connect去判定是否存在就可以了:
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session
.getDefaultInstance(pro, authenticator);
Transport transport = sendMailSession.getTransport();
// 连接邮件smtp服务器,参数分别为服务器地址,用户名和密码
transport.connect(serverHost, userName,
mpassword);
如果没有报错就返回true,说明邮箱确实存在,否则会抛出MessagingException异常。
("mail.transport.protocol", "smtp"); // smtp协议 props.put("mail.smtp.host", mailInfo.getHostName()); props.put("mail.smtp.port", mailInfo.g
邮件,当然如果发送邮件的话也是可以检验出来的,如果邮箱不存在的话,发送是失败的;更简单一点直接用javaMail中的connect去判定是否存在就可以了:// 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMai