2011-01-13 17 views
11

का उपयोग कर ईमेल कैसे भेजें I'm मेरी कंपनी के मेल सर्वर का उपयोग कर एक ईमेल भेजने का प्रयास कर रहा हूं। लेकिन मैं निम्न अपवादएमएस एक्सचेंज सर्वर

Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated 
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) 
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583) 
    at javax.mail.Transport.send0(Transport.java:169) 
    at javax.mail.Transport.send(Transport.java:98) 

यहाँ मेरी नमूना कोड है

Properties props = System.getProperties(); 

// Setup mail server 
props.put("mail.smtp.host", "example.server.com"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.port", "25"); 
// Get session 
//Session session = Session.getDefaultInstance(props, null); 
Session session = Session.getDefaultInstance(props, 
    new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("username", "password"); 
     } 
    }); 

// Define message 
MimeMessage message = new MimeMessage(session); 

// Set the from address 
message.setFrom(new InternetAddress(from)); 

// Set the to address 
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 

// Set the subject 
message.setSubject("Hello JavaMail"); 

// Set the content 
message.setText("Welcome to JavaMail"); 

// Send message 
Transport.send(message); 

कोड किस टुकड़ा गलत है हो रही है? उपयोगकर्ता नाम और पासवर्ड के रूप में, मैं अपनी कंपनी के ईमेल पते और पासवर्ड का उपयोग कर रहा हूं।

उत्तर

6

5.7.1 संभवतः एक्सचेंज के कारण होता है और आपका कोड नहीं। आपको सर्वर पर रिलेइंग करने की आवश्यकता हो सकती है। या तो अज्ञात उपयोगकर्ताओं के लिए या किसी निश्चित आईपी पते से। मैं एक्सचेंज पर एक विशेषज्ञ नहीं हूं लेकिन मुझे यह पहले काम कर रहा है।

हैं जब SMTP के माध्यम से एक ईमेल भेजने के लिए एक एक्सचेंज सर्वर जब उपयोगकर्ता प्रमाणीकृत किया गया है पर कोशिश कर रहा एक 5.7.1 त्रुटि का सामना किया है ..

रेफरी मुद्दे के लिए: यहाँ पिछले समाधान मैं परीक्षण किया है कि काम करता है तुम सिर्फ था Exchange 2007 सर्वर पर एक सेटिंग की वजह से किया गया था - यह सामान्य रूप से 2003 सर्वर

नीचे करके फिक्स्ड पर एक समस्या नहीं होगा ...

आप इस प्रमाणीकरण जीयूआई के माध्यम से स्थापित करने के लिए सेट कर सकते

  • सर्वर विन्यास में/हब परिवहन/डिफ़ॉल्ट < सर्वर नाम >
  • राइट क्लिक करें, गुण, अनुमति समूह
  • चेक "बेनामी उपयोगकर्ताओं" और उसके बाद ठीक

जाहिर है anon उपयोगकर्ताओं को क्लिक भी सुरक्षित नहीं है, लेकिन आप देख सकते हैं कि यह समस्या हल करता है या नहीं।

+0

यह संभव है कि मेरा आईटी व्यवस्थापक एक्सचेंज सर्वर पर एक टेस्ट उपयोगकर्ता बना सकता है और मैं ईमेल भेजने के लिए उपयोग कर सकता हूं? – user509755

+0

मैं नहीं देखता क्यों नहीं। मेरा वेब ऐप एक डोमेन उपयोगकर्ता का प्रतिरूपण करता है जो एक्सचेंज सर्वर से जुड़ता है। मैं एक्सचेंज के बारे में बहुत कुछ नहीं जानता लेकिन ऐप इंस्टॉल करने वाली आखिरी साइट को वेब सर्वर से smtp के माध्यम से रिलेइंग की अनुमति देना पड़ा। इसके बाद मैंने 5.7.1 त्रुटियां प्राप्त करना बंद कर दिया। यदि आप एक उपयोगकर्ता नहीं कर सकते हैं तो आप कम से कम एक मशीन करने में सक्षम होना चाहिए। यहां एक ऐसी समस्या है जिसमें एक उपयोगकर्ता को रिले करने में सक्षम नहीं है जिसमें कुछ दिलचस्प है http://social.technet.microsoft.com/Forums/en-US/exchangesvrtransport/thread/1a84a06a-f1c8-40b4-ace8 -1e264f218aa1/ – WraithNath

+0

मुझे लगता है कि जिस क्षेत्र को आप देखना चाहते हैं वह है 'कनेक्टर प्राप्त करें' – WraithNath

1

मुझे javamail + exchange का उपयोग करना पड़ा। लौटाए गए संदेश असहाय थे। ढेर के लिए धन्यवाद, मुझे कुछ संकेत मिले।

भी प्रयुक्त मशीनों के प्रमाण पत्र को जोड़ने के अपने कोड को यह जोड़े

props.put("mail.smtp.starttls.enable","true"); 

Think। उन्हें ढूंढने के लिए, बस अपने ब्राउज़र पर जाएं, उन्हें निर्यात करें और उपयोग में कैकर्ट फ़ाइल पर आयात करें।

0

बजाय Transport.send(message);

MimeMessage message = new MimeMessage(session); 

message.saveChanges(); 
Transport transport = session.getTransport("smtp"); 
transport.connect(host, "user", "pwd"); 
transport.sendMessage(message, message.getAllRecipients()); 
transport.close(); 

मैं स्थानीय में परीक्षण किया है निम्नलिखित कोड भागों उपयोग करें और यह

+0

'Transport.send (message) 'विधि में ' message.saveChanges() ', ' transport.connect', 'transport.sendMessage' और' transport.close' है इसलिए मुझे नहीं लगता कि मुझे ऐसा करने की आवश्यकता है फिर खुद – omerhakanbilici

2

काम कर रहा है कुछ कंपनियों में करें, Exchange सर्वर एसएमटीपी समर्थन अक्षम है और आप उन्हें पूछना नहीं कर सकते इसे सक्षम करने के लिए। इन मामलों में, एक उचित समाधान इस एक है:

http://davmail.sourceforge.net/

1

Simple Java Mail मेरे लिए काम किया।केवल एक चीज आप की जाँच करने के लिए है सही होस्ट नाम, उपयोगकर्ता नाम, बंदरगाह और पासवर्डTransportStrategy.SMTP_TLS के लिए है:

new Mailer(host, port, username, password, TransportStrategy.SMTP_TLS).sendMail(email); 
+0

@zeddam क्या उपयोग करने के लिए खुला स्रोत है? – Sundhar

2

Mail.jar (संस्करण 1.4.0) एमएस एक्सचेंज के साथ एक संगतता मुद्दा है सर्वर और 530 5.7.1 Client was not authenticated फेंकता है, भले ही उपयोगकर्ता नाम और पासवर्ड कॉन्फ़िगर किया गया हो।

मेल एपीआई को 1.4.4 या 1.4.7 तक अपग्रेड करना समस्या को हल करना चाहिए। निम्न URL से

मेल एपीआई डाउनलोड 1.4.7 हो सकता है: http://www.oracle.com/technetwork/java/javamail/index.html

1

जब मैं एक एमएस exhange एसएमटीपी सर्वर का उपयोग करेगा एक ईमेल भेजने के लिए, मैं ऊपर Maven निर्भरता का उपयोग करें।

<dependency> 
    <groupId>com.microsoft.ews-java-api</groupId> 
    <artifactId>ews-java-api</artifactId> 
    <version>2.0</version> 
</dependency> 

इसी कारण से मैंने एक कक्षा बनाई जो एमएस एक्सचेंज सर्वर के लिए एक ईमेल क्लाइंट का प्रतिनिधित्व करता है। मैं लॉगिंग के लिए log4j का उपयोग करता हूं।

<dependency> 
    <groupId>log4j</groupId> 
    <artifactId>log4j</artifactId> 
    <version>1.2.17</version> 
</dependency> 

एमएस Exchange क्लाइंट वर्ग (मैं धागा सुरक्षा के लिए वस्तु के निर्माण के लिए बिल्डर पैटर्न का उपयोग) के नीचे,

import java.net.URI; 
import java.net.URISyntaxException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import microsoft.exchange.webservices.data.core.ExchangeService; 
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; 
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException; 
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.apache.log4j.Logger; 

/** 
* A client to connect to a MS Exchange SMTP Server. 
*/ 
public final class ExchangeClient { 

    private static final Logger LOGGER = Logger.getLogger(ExchangeClient.class); 

    private final String hostname; 
    private final ExchangeVersion exchangeVersion; 
    private final String domain; 
    private final String username; 
    private final String password; 
    private final String subject; 
    private final String recipientTo; 
    private final List<String> recipientCc; 
    private final List<String> recipientBcc; 
    private final List<String> attachments; 
    private final String message; 

    private ExchangeClient(ExchangeClientBuilder builder) { 
     this.hostname = builder.hostname; 
     this.exchangeVersion = builder.exchangeVersion; 
     this.domain = builder.domain; 
     this.username = builder.username; 
     this.password = builder.password; 
     this.subject = builder.subject; 
     this.recipientTo = builder.recipientTo; 
     this.recipientCc = builder.recipientCc; 
     this.recipientBcc = builder.recipientBcc; 
     this.attachments = builder.attachments; 
     this.message = builder.message; 
    } 

    public static class ExchangeClientBuilder { 

     private String hostname; 
     private ExchangeVersion exchangeVersion; 
     private String domain; 
     private String username; 
     private String password; 
     private String subject; 
     private String recipientTo; 
     private List<String> recipientCc; 
     private List<String> recipientBcc; 
     private List<String> attachments; 
     private String message; 

     public ExchangeClientBuilder() { 
      this.exchangeVersion = ExchangeVersion.Exchange2010_SP1; 
      this.hostname = ""; 
      this.username = ""; 
      this.password = ""; 
      this.subject = ""; 
      this.recipientTo = ""; 
      this.recipientCc = new ArrayList<>(0); 
      this.recipientBcc = new ArrayList<>(0); 
      this.attachments = new ArrayList<>(0); 
      this.message = ""; 
     } 

     /** 
     * The hostname of the Exchange Web Service. It will be used for 
     * connecting with URI https://hostname/ews/exchange.asmx 
     * 
     * @param hostname the hostname of the MS Exchange Smtp Server. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder hostname(String hostname) { 
      this.hostname = hostname; 
      return this; 
     } 

     /** 
     * The Exchange Web Server version. 
     * 
     * @param exchangeVersion the Exchange Web Server version. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder exchangeVersion(ExchangeVersion exchangeVersion) { 
      this.exchangeVersion = exchangeVersion; 
      return this; 
     } 

     /** 
     * The domain of the MS Exchange Smtp Server. 
     * 
     * @param domain the domain of the Active Directory. The first part of 
     * the username. For example: MYDOMAIN\\username, set the MYDOMAIN. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder domain(String domain) { 
      this.domain = domain; 
      return this; 
     } 

     /** 
     * The username of the MS Exchange Smtp Server. The second part of the 
     * username. For example: MYDOMAIN\\username, set the username. 
     * 
     * @param username the username of the MS Exchange Smtp Server. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder username(String username) { 
      this.username = username; 
      return this; 
     } 

     /** 
     * The password of the MS Exchange Smtp Server. 
     * 
     * @param password the password of the MS Exchange Smtp Server. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder password(String password) { 
      this.password = password; 
      return this; 
     } 

     /** 
     * The subject for this send. 
     * 
     * @param subject the subject for this send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder subject(String subject) { 
      this.subject = subject; 
      return this; 
     } 

     /** 
     * The recipient for this send. 
     * 
     * @param recipientTo the recipient for this send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder recipientTo(String recipientTo) { 
      this.recipientTo = recipientTo; 
      return this; 
     } 

     /** 
     * You can specify one or more email address that will be used as cc 
     * recipients. 
     * 
     * @param recipientCc the first cc email address. 
     * @param recipientsCc the other cc email address for this send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder recipientCc(String recipientCc, String... recipientsCc) { 
      // Prepare the list. 
      List<String> recipients = new ArrayList<>(1 + recipientsCc.length); 
      recipients.add(recipientCc); 
      recipients.addAll(Arrays.asList(recipientsCc)); 
      // Set the list. 
      this.recipientCc = recipients; 
      return this; 
     } 

     /** 
     * You can specify a list with email addresses that will be used as cc 
     * for this email send. 
     * 
     * @param recipientCc the list with email addresses that will be used as 
     * cc for this email send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder recipientCc(List<String> recipientCc) { 
      this.recipientCc = recipientCc; 
      return this; 
     } 

     /** 
     * You can specify one or more email address that will be used as bcc 
     * recipients. 
     * 
     * @param recipientBcc the first bcc email address. 
     * @param recipientsBcc the other bcc email address for this send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder recipientBcc(String recipientBcc, String... recipientsBcc) { 
      // Prepare the list. 
      List<String> recipients = new ArrayList<>(1 + recipientsBcc.length); 
      recipients.add(recipientBcc); 
      recipients.addAll(Arrays.asList(recipientsBcc)); 
      // Set the list. 
      this.recipientBcc = recipients; 
      return this; 
     } 

     /** 
     * You can specify a list with email addresses that will be used as bcc 
     * for this email send. 
     * 
     * @param recipientBcc the list with email addresses that will be used 
     * as bcc for this email send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder recipientBcc(List<String> recipientBcc) { 
      this.recipientBcc = recipientBcc; 
      return this; 
     } 

     /** 
     * You can specify one or more email address that will be used as cc 
     * recipients. 
     * 
     * @param attachment the first attachment. 
     * @param attachments the other attachments for this send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder attachments(String attachment, String... attachments) { 
      // Prepare the list. 
      List<String> attachmentsToUse = new ArrayList<>(1 + attachments.length); 
      attachmentsToUse.add(attachment); 
      attachmentsToUse.addAll(Arrays.asList(attachments)); 
      // Set the list. 
      this.attachments = attachmentsToUse; 
      return this; 
     } 

     /** 
     * You can specify a list with email attachments that will be used for 
     * this email send. 
     * 
     * @param attachments the list with email attachments that will be used 
     * for this email send. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder attachments(List<String> attachments) { 
      this.attachments = attachments; 
      return this; 
     } 

     /** 
     * The body of the email message. 
     * 
     * @param message the body of the email message. 
     * @return the builder for chain usage. 
     */ 
     public ExchangeClientBuilder message(String message) { 
      this.message = message; 
      return this; 
     } 

     /** 
     * Build a mail. 
     * 
     * @return an EmailApacheUtils object. 
     */ 
     public ExchangeClient build() { 
      return new ExchangeClient(this); 
     } 
    } 

    public boolean sendExchange() { 
     // The Exchange Server Version. 
     ExchangeService exchangeService = new ExchangeService(exchangeVersion); 

     // Credentials to sign in the MS Exchange Server. 
     ExchangeCredentials exchangeCredentials = new WebCredentials(username, password, domain); 
     exchangeService.setCredentials(exchangeCredentials); 

     // URL of exchange web service for the mailbox. 
     try { 
      exchangeService.setUrl(new URI("https://" + hostname + "/ews/Exchange.asmx")); 
     } catch (URISyntaxException ex) { 
      LOGGER.error("An exception occured while creating the uri for exchange service.", ex); 
      return false; 
     } 

     // The email. 
     EmailMessage emailMessage; 
     try { 
      emailMessage = new EmailMessage(exchangeService); 
      emailMessage.setSubject(subject); 
      emailMessage.setBody(MessageBody.getMessageBodyFromText(message)); 
     } catch (Exception ex) { 
      LOGGER.error("An exception occured while setting the email message.", ex); 
      return false; 
     } 

     // TO recipient. 
     try { 
      emailMessage.getToRecipients().add(recipientTo); 
     } catch (ServiceLocalException ex) { 
      LOGGER.error("An exception occured while sstting the TO recipient(" + recipientTo + ").", ex); 
      return false; 
     } 

     // CC recipient. 
     for (String recipient : recipientCc) { 
      try { 
       emailMessage.getCcRecipients().add(recipient); 
      } catch (ServiceLocalException ex) { 
       LOGGER.error("An exception occured while sstting the CC recipient(" + recipient + ").", ex); 
       return false; 
      } 
     } 

     // BCC recipient 
     for (String recipient : recipientBcc) { 
      try { 
       emailMessage.getBccRecipients().add(recipient); 
      } catch (ServiceLocalException ex) { 
       LOGGER.error("An exception occured while sstting the BCC recipient(" + recipient + ").", ex); 
       return false; 
      } 
     } 

     // Attachements. 
     for (String attachmentPath : attachments) { 
      try { 
       emailMessage.getAttachments().addFileAttachment(attachmentPath); 
      } catch (ServiceLocalException ex) { 
       LOGGER.error("An exception occured while setting the attachment.", ex); 
       return false; 
      } 
     } 

     try { 
      emailMessage.send(); 
      LOGGER.debug("An email is send."); 
     } catch (Exception ex) { 
      LOGGER.error("An exception occured while sending an email.", ex); 
      return false; 
     } 

     return true; 
    } 

} 

एक काम कर उदाहरण के लिए,

// import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; 
ExchangeClient client = new ExchangeClient.ExchangeClientBuilder() 
     .hostname("webmail.domainOfWeb.com") 
     .exchangeVersion(ExchangeVersion.Exchange2010) 
     .domain("ActiveDirectoryDomain") 
     .username("ActiveDirectoryUsername") 
     .password("ActiveDirectoryPassword") 
     .recipientTo("[email protected]") 
     .recipientCc("[email protected]") // Ignore it in case you will not use Cc recipients. 
     .recipientBcc("[email protected]") // Ignore it in case you will not use Bcc recipients. 
     .attachments("/home/username/image.png") // Ignore it in case you will not use attachements. 
     .subject("Test Subject") 
     .message("Test Message") 
     .build(); 
client.sendExchange(); 
संबंधित मुद्दे