求一个用JAVA实现邮件收发的程序

2025-03-10 05:43:25
推荐回答(3个)
回答1:

package test.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JMailSend {
public static void main(String[] args) {
String smtpHost = "smtp.163.com";//邮件服务器
String from = "jianzicheng2@163.com";
String to = "jianzicheng@gmail.com";
String userName = "jianzicheng2";
String password = "7931725";
SmtpAuth auth = null;
//获取系统属性
Properties props = System.getProperties();
auth =new SmtpAuth();
auth.setUserinfo(userName,password);
//auth=new PasswordAuthentication("","");

//设置邮件服务器
props.put("mail.smtp.host",smtpHost);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","25");

//下面应用对本应用而言不是必须的
//props.put("mail.transport.protocol","smtp");
//props.put("mail.store.protocol","imap");
//props.put("mail.smtp.class","com.sun.mail.smtp.SMTPTransport");
//props.put("mail.imap.class","com.sun.mail.imap.IMAPStore");

//获取会话对象
Session session = Session.getDefaultInstance(props,auth);
session.setPasswordAuthentication(new URLName(smtpHost),auth.getPasswordAuthentication());

//定义message
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("JMail Test Application");
message.setText("You JMail Application is sucessful!");

//发送消息
Transport.send(message);
System.out.println("发送成功!");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}

}

}

package test.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class SmtpAuth extends Authenticator{
private String user,password;
public void setUserinfo(String getuser,String getpassword){
user = getuser;
password = getpassword;
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
}

回答2:

给你发一个咯
我的邮箱是wgxdfjr0328@163.com

回答3:

JMail.jar