求一个javaweb邮件收发系统eclipse源代码,tomcat可以运行的。
package me.gacl.main;
成都创新互联公司专注于万载企业网站建设,响应式网站开发,商城网站制作。万载网站建设公司,为万载等地区提供建站服务。全流程按需设计网站,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 创建一封只包含文本的邮件
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//邮件的标题
message.setSubject("只包含文本的简单邮件");
//邮件的文本内容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//返回创建好的邮件对象
return message;
}
}
java实现接收邮件,mail.jar
这个异常表示有类型转换错误,有个地方需要使用 javax.mail.Multipart 类型的对象,你用了 com.sun.mail.imap.IMAPInputStream 类型,所以出现了这个异常。
根据你给的有限的资料,暂时只能分析道这么多了。
用java写收发邮件的程序,求助,在线
import java.util.Properties;
import java.util.Date;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
public class HelloWorldJavaMail {
public static void main(String[] args) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host","xxx.xxx.xxx.xxx");
Session session=Session.getDefaultInstance(props,null);
MimeMessage mimeMessage=new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("xx@xx.xx","姓名","gb2312"));
mimeMessage.setRecipients(Message.RecipientType.TO,"xx@xx.xx");
mimeMessage.setSubject("Hello World JavaMail","gb2312");
mimeMessage.setText("h1Hello World JavaMail/h1","gb2312");
mimeMessage.setHeader("Content-Type","text/html");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2。收信
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class HelloWorldPOP {
public static void main(String args[]) {
try {
String host="xxx.xxx.xxx.xxx";
String user="xxxxxx";
String password="xxxxxx";
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect(host, -1, user, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
int totalMessages = folder.getMessageCount();
if (totalMessages == 0) {
System.out.println("没有邮件");
folder.close(false);
store.close();
return;
}
Message[] messages = folder.getMessages();
for (int i = 0; i messages.length; i++) {
showMessage(messages[i]);
}
folder.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void showMessage(Message m) throws Exception {
Address[] a;
if ((a = m.getFrom())!=null) {
for (int j = 0; j a.length; j++) {
System.out.println("送信人: " + MimeUtility.decodeText(a[j].toString()));
}
}
if ((a = m.getRecipients(Message.RecipientType.TO))!=null) {
for (int j = 0; j a.length; j++) {
System.out.println("收信人: " + MimeUtility.decodeText(a[j].toString()));
}
}
System.out.println("主体: " + m.getSubject());
Date d = m.getSentDate();
System.out.println("日期: " + (d!= null ? d.toString() : "不明"));
System.out.println("大小: " + m.getSize());
System.out.println("内容:\n" + m.getContent());
}
}
用JAVAMAIL很容易实现的。
参照:
本文名称:java邮件接收代码 java收发邮件
URL网址:http://lswzjz.com/article/dojhjje.html