「java连接邮箱」java邮件
本篇文章给大家谈谈java连接邮箱,以及java邮件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java怎么连接exchange收发邮件
- 2、java中如何实现公司邮箱发送邮件配置
- 3、如何使用Java发送qq邮件
- 4、java mail 发邮件连接不上smtp服务器怎么办
- 5、如何在 java 发邮件中提供链接?
- 6、java如何使用ssl连接qq邮箱
java怎么连接exchange收发邮件
/**
serverName 接收邮件地址
user 用户信息
pwd 密码
path 邮件临时储存路径
max 每次接收邮件的最大数量
**/
public int receive(String serverName, String user, String pwd, String path, int max) throws Exception {
//新建ExchangeVersion.Exchange2007_SP1版本的Exchange服务
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
String[] userInfo = user.split("/");
//用户认证信息
ExchangeCredentials credentials = new WebCredentials(userInfo[1], pwd,userInfo[0]);
service.setCredentials(credentials);
//设置Exchange连接的服务器地址
service.setUrl(new URI(serverName));
//绑定邮箱
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
//获取邮箱文件数量
int count = inbox.getTotalCount();
if(max 0) count = count max ? max : count;
//循环获取邮箱邮件
ItemView view = new ItemView(count);
FindItemsResultsItem findResults = service.findItems(inbox.getId(), view);
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId());
ListAttachment attachs = message.getAttachments().getItems();
try{
if(message.getHasAttachments()){
for(Attachment f : attachs){
if(f instanceof FileAttachment){
//接收邮件到临时目录
File tempZip = new File(path,f.getName());
((FileAttachment)f).load(tempZip.getPath());
}
}
//删除邮件
message.delete(DeleteMode.HardDelete);
}
}catch(Exception err){
log.equals(err);
}
}
return count;
}
java中如何实现公司邮箱发送邮件配置
Java中可以通过Javamail API实现公司邮箱邮件发送配置,Java mail是利用现有的邮箱账户发送邮件的工具,具体步骤如如下:
1、通过JavamailAPI设置发送者邮箱用户名及密码
2、通过JavamailAPI设置邮件主题、邮件内容、附件及邮件发送时间
3、通过JavamailAPI设置发送者邮箱地址及接收者邮箱地址,接收者地址可以是多个及抄送
4、邮件的需基本元素都设置完毕后,即可通过Javamail API的发送接口执行发送操作。
如何使用Java发送qq邮件
方法:
1.前提准备工作:
首先,邮件的发送方要开启POP3 和SMTP服务--即发送qq邮件的账号要开启POP3 和SMTP服务
2.开启方法:
登陆qq邮箱
3.点击 设置
4.点击—-账户
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启
6.送短信 —–点击确定
7.稍等一会,很得到一个授权码! –注意:这个一定要记住,一会用到
8.点击保存修改 —OK 完成
9.java 测试代码:
package cn.cupcat.test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息
//得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
//设置发件人邮箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//设置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//设置邮件标题
message.setSubject("这是第一封Java邮件");
//设置邮件内容
message.setText("内容为: 这是第一封java发送来的邮件。");
//得到邮差对象
Transport transport = session.getTransport();
//连接自己的邮箱账户
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密码为刚才得到的授权码
//发送邮件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.运行就会发出邮件了。。。。
下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的,你们测试的时候,可以修改能你们自己的邮箱。最后,祝你也能成功,如果有什么问题,可以一起讨论!
注意事项
得到的授权码一定要保存好,程序中要使用
java mail 发邮件连接不上smtp服务器怎么办
SMTP 的是“Simple Mail Transfer Protocol”,即简单邮件传输协议。是一组用于从源地址到目的地址传输邮件的规范,通过控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。 SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。 增加 SMTP 认证的目的是为了使用户避免受到垃圾邮件的侵扰。
(1)可以尝试换一个邮箱服务器试一试.这个是可以连接上的
如何在 java 发邮件中提供链接?
代码如下:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailTest {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.163.com");
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("xxx","xxx");//这里分别填写发送email的用户名、密码
}
}
);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("xxx"));//这里是发送方的email地址如:xxx@163.com
msg.setSubject("test javamail");
msg.setRecipients(RecipientType.TO,
InternetAddress.parse("xxx"));//这里是接收方的email地址如:xxx@163.com
msg.setContent("a href=\"\"谷歌/a","text/html;charset=gb2312");
Transport.send(msg);
}
}
java如何使用ssl连接qq邮箱
Gmail目前已经启用了POP3和SMTP服务,与其他邮箱不同的是Gmail提供的POP3和SMTP是使用安全套接字层SSL的,因此常规的JavaMail程序是无法收发邮件的,下面是使用JavaMail如何收取Gmail邮件以及发送邮件的代码:
1. [代码]GmailFetch.java 跳至 [1] [2] [全屏预览]
01 package lius.javamail.ssl;
02
03 import java.io.UnsupportedEncodingException;
04 import java.security.*;
05 import java.util.Properties;
06 import javax.mail.*;
07 import javax.mail.internet.InternetAddress;
08 import javax.mail.internet.MimeUtility;
09
10 /**
11 * 用于收取Gmail邮件
12 * @author Winter Lau
13 */
14 public class GmailFetch {
15
16 public static void main(String argv[]) throws Exception {
17
18 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
19 final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
20
21 // Get a Properties object
22 Properties props = System.getProperties();
23 props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
24 props.setProperty("mail.pop3.socketFactory.fallback", "false");
25 props.setProperty("mail.pop3.port", "995");
26 props.setProperty("mail.pop3.socketFactory.port", "995");
27
28 //以下步骤跟一般的JavaMail操作相同
29 Session session = Session.getDefaultInstance(props,null);
30
31 //请将红色部分对应替换成你的邮箱帐号和密码
32 URLName urln = new URLName("pop3","pop.gmail.com",995,null,
33 "[邮箱帐号]", "[邮箱密码]");
34 Store store = session.getStore(urln);
35 Folder inbox = null;
36 try {
37 store.connect();
38 inbox = store.getFolder("INBOX");
39 inbox.open(Folder.READ_ONLY);
40 FetchProfile profile = new FetchProfile();
41 profile.add(FetchProfile.Item.ENVELOPE);
42 Message[] messages = inbox.getMessages();
43 inbox.fetch(messages, profile);
44 System.out.println("收件箱的邮件数:" + messages.length);
45 for (int i = 0; i messages.length; i++) {
46 //邮件发送者
47 String from = decodeText(messages[i].getFrom()[0].toString());
48 InternetAddress ia = new InternetAddress(from);
49 System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
50 //邮件标题
51 System.out.println("TITLE:" + messages[i].getSubject());
52 //邮件大小
53 System.out.println("SIZE:" + messages[i].getSize());
54 //邮件发送时间
55 System.out.println("DATE:" + messages[i].getSentDate());
56 }
57 } finally {
58 try {
59 inbox.close(false);
60 } catch (Exception e) {}
61 try {
62 store.close();
63 } catch (Exception e) {}
64 }
65 }
66
67 protected static String decodeText(String text)
68 throws UnsupportedEncodingException {
69 if (text == null)
70 return null;
71 if (text.startsWith("=?GB") || text.startsWith("=?gb"))
72 text = MimeUtility.decodeText(text);
73 else
74 text = new String(text.getBytes("ISO8859_1"));
75 return text;
76 }
77
78 }
2. [代码]GmailSender.java
01 package lius.javamail.ssl;
02
03 import java.security.Security;
04 import java.util.Date;
05 import java.util.Properties;
06
07 import javax.mail.Authenticator;
08 import javax.mail.Message;
09 import javax.mail.MessagingException;
10 import javax.mail.PasswordAuthentication;
11 import javax.mail.Session;
12 import javax.mail.Transport;
13 import javax.mail.internet.AddressException;
14 import javax.mail.internet.InternetAddress;
15 import javax.mail.internet.MimeMessage;
16
17 /**
18 * 使用Gmail发送邮件
19 * @author Winter Lau
20 */
21 public class GmailSender {
22
23 public static void main(String[] args) throws AddressException, MessagingException {
24 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
25 final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
26 // Get a Properties object
27 Properties props = System.getProperties();
28 props.setProperty("mail.smtp.host", "smtp.gmail.com");
29 props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
30 props.setProperty("mail.smtp.socketFactory.fallback", "false");
31 props.setProperty("mail.smtp.port", "465");
32 props.setProperty("mail.smtp.socketFactory.port", "465");
33 props.put("mail.smtp.auth", "true");
34 final String username = "[邮箱帐号]";
35 final String password = "[邮箱密码]";
36 Session session = Session.getDefaultInstance(props, new Authenticator(){
37 protected PasswordAuthentication getPasswordAuthentication() {
38 return new PasswordAuthentication(username, password);
39 }});
40
41 // -- Create a new message --
42 Message msg = new MimeMessage(session);
43
44 // -- Set the FROM and TO fields --
45 msg.setFrom(new InternetAddress(username + "@mo168.com"));
46 msg.setRecipients(Message.RecipientType.TO,
47 InternetAddress.parse("[收件人地址]",false));
48 msg.setSubject("Hello");
49 msg.setText("How are you");
50 msg.setSentDate(new Date());
51 Transport.send(msg);
52
53 System.out.println("Message sent.");
54 }
55 }
关于java连接邮箱和java邮件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-07,除非注明,否则均为
原创文章,转载请注明出处。