Java通过MS Exchange发送邮件(moyosoft解决方案)

目前公司开发的系统要通过客户的Exchange Server发送邮件,在网上找了许多方案,目前使用http://www.moyosoft.com/jbex/的Java Bridge to Exchange解决了该问题,现将代码实现贴出来。


SendMail.java



public class SendMail

{

public static void main(String[] args) throws ExchangeServiceException

{

// To provide hostname and credentials information, use:

Exchange exchange = new Exchange("hostname",                                                             "username", "password", "domain", true);


if(exchange == null)

{

return;

}


// Create a new e-mail:

ExchangeMail mail = exchange.createMail();


// Set the recipient, subject and body:

mail.setToRecipient("wclwen@163.com");

mail.setSubject("Test message");

mail.setBody("Hello. There's a message sent from Java.");

// Send the message:

mail.send();

}

}

 

 

因为该软件只有一个月的试用期,所以要想长期使用,需要在注册表中修改install_date时间。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用Exchange Online发送邮件,你可以使用JavaMail API来完成。下面是一个简单的示例代码: ```java import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class ExchangeOnlineEmailSender { public static void main(String[] args) { // 配置SMTP服务器和认证信息 Properties props = new Properties(); props.put("mail.smtp.host", "smtp.office365.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // 设置发件人邮箱和密码 final String username = "your_email@your_domain.com"; final String password = "your_password"; // 创建会话 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(username, password); } }); try { // 创建邮件消息 Message message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress("your_email@your_domain.com")); // 设置收件人 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@recipient_domain.com")); // 设置邮件主题 message.setSubject("Hello from JavaMail"); // 设置邮件正文 message.setText("This is a test email from JavaMail API"); // 发送邮件 Transport.send(message); System.out.println("Email sent successfully!"); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 请注意,你需要将代码中的`your_email@your_domain.com`替换为你的发件人邮箱地址,`your_password`替换为你的邮箱密码,`recipient_email@recipient_domain.com`替换为接收邮件的收件人邮箱地址。 此代码使用SMTP协议通过Exchange Online发送邮件。它会连接到Office 365的SMTP服务器,并使用提供的用户名和密码进行身份验证。确保你已经启用了Exchange Online中的SMTP并拥有有效的SMTP凭据。 此外,确保你已经包含了JavaMail API库,可以从JavaMail官方网站下载并添加到你的项目中。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值