我们使用EWS Java API在Java应用程序上使用outlook日历.我在EWS上遇到了身份验证问题.
我尝试了由rackspace提供的云Outlook帐户上的应用程序,一切正常,所以我知道凭据是准确的.
这是代码:
import java.net.URI;
import java.net.URISyntaxException;
import microsoft.exchange.webservices.data.*;
public class TestClass {
public static void main(String[] args) {
TestClass obj = new TestClass();
obj.testMethod();
}
public void testMethod() {
ExchangeService service = new ExchangeService(
ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials = new WebCredentials("username",
"password");
service.setCredentials(credentials);
try {
service.setUrl(new URI("https://domain/EWS/Exchange.asmx"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
EmailMessage msg;
try {
msg = new EmailMessage(service);
msg.setSubject("hello world");
msg.setBody(MessageBody
.getMessageBodyFromText("Sent using the EWS API"));
msg.getToRecipients().add("test@test.com");
msg.send();
} catch (Exception e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我尝试编写一个访问Exchange Web服务的Java应用程序,以便阅读电子邮件.因此,我使用EWSMicrosoft提供的Exchange Web Services()Java API.
我已经遇到了几个问题,我终于发现应该使用LDAP完成身份验证.不幸的是,我不知道怎么做这样的事情.EWS API是否允许配置连接到Exchange服务器时要使用的身份验证方案?如果是,如何配置?
这是我用于连接的代码,但它使用默认的身份验证方案,即NTLM:
String url = "https//my-server/EWS/exchange.asmx";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.setTraceEnabled(true);
service.setCredentials(new WebCredentials("user", "password"));
service.setUrl(url.toURI());
Mailbox mailbox = new Mailbox("foo@bar.com");
FolderId folder = new FolderId(WellKnownFolderName.Inbox, mailbox);
ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
FindItemsResults<Item> items = service.findItems(folder, view);
Run Code Online (Sandbox Code Playgroud) 我正在测试https://javaee.github.io/javamail/FAQ中所述的sendmail示例 .这是一个简单的邮件发送操作,没有SSL,也没有附件.它在任何地方都可以正常工作,但在客户端上,当邮件api变得更新时,我们会有一个去线性能.
服务器是windows,java 1.8.131,在所有测试用例中使用相同的邮件服务器和邮件帐户以及相同的地址和java源.唯一的区别是使用的mailapi.我运行程序
java -cp javax.mail.1.XXjar; sendmailtesttool.jar SendMail
我为每个邮件api尝试了100次并获得平均持续时间.
使用的来源:
Properties properties = new Properties();
properties.put("mail.smtp.host", 192.168.0.X); // use default port25
Session session = Session.getInstance(properties);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(..);
msg.setSubject(..);
msg.setRecipients(..);
//no attachments
MultiPart mp = new MultiPart();
MimeBodyPart bp = new MimeBodyPart();
DataSource ds = ByteArrayDataSource("foo"..);
DataHandler dh = new DataHandler(ds);
bp.setDataHandler(dh);
mp.addBodyPart(bp);
msg.setContent(mp);
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
msg.saveChanges(); // is really needed ? (no reply/forward) …Run Code Online (Sandbox Code Playgroud) 我尝试使用microsoft Exchange服务器在本地网络中发送带有java的电子邮件
有我的代码:
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Main {
public static void main(String[] args) {
final String username = "username@MyDomain.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", "exchange_host.MyDomain.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth.mechanisms","NTLM");
props.put("mail.smtp.auth.ntlm.domain","MyDomain");
Session session = Session.getInstance(props,new MyAuthenticator(username,password));
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from_adress@MyDomain.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipent_adresse"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ …Run Code Online (Sandbox Code Playgroud)