java exchange发邮件_Java通过exchange协议发送邮件

本文实例为大家分享了Java通过exchange协议发送邮件的具体代码,供大家参考,具体内容如下

pom.xml 导入包

com.microsoft.ews-java-api

ews-java-api

2.0

application.properties 配置信息

#邮箱地址

youjia.exchange.mail.username=123@abc.com

#邮箱密码

youjia.exchange.mail.password=123456

#邮箱exchange服务地址,如果不知道找运维

youjia.exchange.mail.host=https://*****/ews/exchange.asmx

代码

package com.youjia.found.manager;

import com.youjia.found.common.util.Check;

import microsoft.exchange.webservices.data.core.ExchangeService;

import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;

import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;

import microsoft.exchange.webservices.data.core.service.item.EmailMessage;

import microsoft.exchange.webservices.data.credential.ExchangeCredentials;

import microsoft.exchange.webservices.data.credential.WebCredentials;

import microsoft.exchange.webservices.data.property.complex.MessageBody;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

import javax.mail.internet.InternetAddress;

import java.net.URI;

/**

*

exchange邮件处理类

*

* @author eric

* @date 2020/2/6 11:08 AM

* @since

*/

@Component

public class MailExchangeManager {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Value("${youjia.exchange.mail.username}")

private String username ;

@Value("${youjia.exchange.mail.password}")

private String password;

@Value("${youjia.exchange.mail.host}")

private String host ;

/**

* 使用Exchange协议发送

* @param to 收件人

* @param subject 邮件主题

* @param content 正文

* @param filePath 附件

*

* @throws Exception

*/

public boolean sendMail(String to, String subject, String content, String filePath) {

logger.info("exchange邮件发送 to:{}, subject:{}, content:{},filePath:{}", to, subject, content,filePath);

boolean isOK=false;

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

ExchangeCredentials credentials = new WebCredentials(username,password);

service.setCredentials(credentials);

try {

service.setUrl(new URI(host));

EmailMessage msg = new EmailMessage(service);

msg.setSubject(subject);

MessageBody body = MessageBody.getMessageBodyFromText(content);

body.setBodyType(BodyType.HTML);

msg.setBody(body);

//支持多个收件人

InternetAddress[] addresses = InternetAddress.parse(to);

for (InternetAddress address : addresses) {

msg.getToRecipients().add(address.getAddress());

}

if (Check.notEmpty(filePath)) {

msg.getAttachments().addFileAttachment(filePath);

}

msg.send();

isOK=true;

} catch (Exception e) {

logger.error(e.getMessage(),e);

isOK= false;

}

return isOK;

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 发送 Exchange 邮箱邮件的示例代码: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class ExchangeMailSender { public static void main(String[] args) { final String username = "your_username"; final String password = "your_password"; final String recipientEmail = "recipient_email_address"; final String subject = "Test Email"; final String body = "This is a test email sent from Java."; Properties props = new Properties(); props.put("mail.smtp.host", "your_exchange_server"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.port", "587"); // Change to the appropriate port number Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail)); message.setSubject(subject); message.setText(body); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { System.out.println("Failed to send email. Error message: " + e.getMessage()); } } } ``` 请将代码中的以下信息替换为您自己的信息: - `your_username`:您的 Exchange 邮箱用户名 - `your_password`:您的 Exchange 邮箱密码 - `recipient_email_address`:收件人的邮箱地址 - `your_exchange_server`:您的 Exchange 邮箱服务器域名或 IP 地址 请注意,此代码需要 JavaMail API 和 Exchange Web Services Java API。您需要将这些 API 添加到您的项目中才能运行此代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值