Hallo,
Ich habe eine Mailklasse, die funktionert prima, solange die Exchange SMTP ohne Authentifizierung zulassen. Nun habe ich einen Exchange 2007, der liefert mir immer ein 5.7,1 "Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated" Was mache ich falsch? Der Focus liegt auf der Methode prepare (ist fett hervorgehoben).

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Created on 20.05.2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.j2dot;
 
 
 
import java.util.Properties;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
import com.j2dot.exceptions.Exceptions;
import com.j2dot.exceptions.J2InternalException;
import com.j2dot.web.utils.ConfigUtils;
 
/**
 * Utility class,  sends emails. * 
 *
 */
public class MailUtils {
 
    /**
     * sends email in plain text.
     * @param server name or ip of the SMTP server.
     * @param subject Subject.
     * @param text  Body of the email.
     * @param from  'from' address.
     * @param to    'to'  address
     * @throws J2InternalException  any internal exception.
     */
    public static void sendPlainText(String server,String subject, String text, String from , String to) throws J2InternalException{
        sendPlainText( server, subject,  text,  from ,  to, null);
    }
    /**
     * sends email as simple text.
     * @param server  server name or ip of the SMTP server.
     * @param subject the Subject.
     * @param text  the body.
     * @param from
     * @param to
     * @param replyto can be null.
     * @throws J2InternalException
     */
    public static void sendPlainText(String server,String subject, String text, String from , String to,String replyto) throws J2InternalException{
        MimeMessage message=null;
        try {
             message=prepare( server, subject, from ,  to, replyto);
                    message.setText(text);
 
        } catch (AddressException e) {
            Exceptions.throwInternalException(e);
        } catch (MessagingException e) {
            Exceptions.throwInternalException(e);
        }
 
 
                try {
                    Transport.send(message);
                } catch (MessagingException e1) {
                    Exceptions.throwInternalException(e1);
                }
    }
 
    /**
     * sends email as simple text with selectable encoding
     * @param server  server name or IP of the SMTP server.
     * @param subject subject
     * @param text    body
     * @param from    sender address
     * @param to      receiver address
     * @param charset charset for encoding
     * @throws J2InternalException
     */
    public static void sendPlainTextEncoded(String server,String subject, String text, String from , String to, String charset) throws J2InternalException{
        sendPlainTextEncoded(server, subject, text, from, to, null, charset);
    }
 
    /**
     * sends email as simple text with selectable encoding
     * @param server  server name or IP of the SMTP server.
     * @param subject subject
     * @param text    body
     * @param from    sender address
     * @param to      receiver address
     * @param replyto reply address, can be null
     * @param charset charset for encoding
     * @throws J2InternalException
     */
    public static void sendPlainTextEncoded(String server,String subject, String text, String from , String to,String replyto, String charset) throws J2InternalException{
        MimeMessage message = null;
        try {
            message = prepare(server, subject, from, to, replyto);
            message.setText(text, charset);
        } catch (AddressException ae) {
            Exceptions.throwInternalException(ae);
        } catch (MessagingException me) {
            Exceptions.throwInternalException(me);
        }
        try {
            Transport.send(message);
        } catch (MessagingException me) {
            Exceptions.throwInternalException(me);
        }
    }
 
 
    /**
     * creates the Email message.
     * @param server  Ip address or the name of the mail server.
     * @param subject  the subject of the message.
     * @param from  the from adress.
     * @param to  the address of the recipient(s), can be a comma separated string with addresses.
     * If an address starts with Cc: oder Bcc: it is used as CC oder BCC address.
     * @param replyto  the reply-to address, can be null.
     * @return the message.
     * @throws AddressException
     * @throws MessagingException
     */
    public static MimeMessage prepare(String server,String subject,String from , String to,String replyto) throws AddressException, MessagingException{
        String smtp_username = "";
        String smtp_password = "";
        Authenticator auth1 = null;
        Properties props = System.getProperties();
        String port = ConfigUtils.getString("smtpport");
//Ist in meinem Fall jetzt true     
[B]Boolean auth = ConfigUtils.getBoolean("smtp_auth");[/B]      
//Das wird ebenfalls durchlaufen        
[B]if (auth.equals(true)) {
            smtp_username = ConfigUtils.getString("smtpuser");
            smtp_password = ConfigUtils.getString("smtppassword");          
        }
        
        props.put("mail.smtp.host", server);
        if(Validator.notEmpty(port)) {          
            props.put("mail.smtp.port", port);      
        }
        if(auth.equals(true)) {         
//PasswordAuthentication(username, password) ist Return von auth1. Benutzername und Passwort im Klartext.           
auth1 = new PopupAuthenticator();
            props.put("mail.smtp.auth", "true");            
        }
        
        // encoding for mail subject
        String charSet = ConfigUtils.getString("default_mail_encoding");
        if(!Validator.notEmpty(charSet)) {
            charSet = "US-ASCII"; // default 
        }
        System.out.println("auth1:"+auth1);
        Session session = Session.getDefaultInstance(props, auth1); 
        MimeMessage message = new MimeMessage(session);
        
        //Nur wenn authentifiziertes SMTP erforderlich ist
        
        /*
        if (auth.equals(true)) {
            Transport tr = session.getTransport("smtp");
            tr.connect(server,smtp_username,smtp_password);
            message.saveChanges();  
        }
        */
            message.setFrom(new InternetAddress(from));
            if(replyto!=null){
                message.addHeader("Reply-To",replyto);
            }
            String[] tos=to.split("\\,");
            for(int i=0;i<tos.length;i++){
                if(tos[i].toLowerCase().startsWith("bcc:")){
                    message.addRecipient(Message.RecipientType.BCC,
                              new InternetAddress(tos[i].substring(4)));
                    continue;
                }
                if(tos[i].toLowerCase().startsWith("cc:")){
                    message.addRecipient(Message.RecipientType.CC,
                              new InternetAddress(tos[i].substring(3)));
                    continue;
                }
            message.addRecipient(Message.RecipientType.TO,
                      new InternetAddress(tos[i]));
            }
                    message.setSubject(subject, charSet);
                    return message;
    }[/B]
 
 
    /**
     * Sends Email as html.
     * @param server Ip address or the name of the mail server.
     * @param subject the Subject
     * @param text  the text (HTML).
     * @param from
     * @param to
     * @param replyto  can be null.
     * @throws J2InternalException
     */
    public static void sendHtml(String server,String subject, String text, String from , String to,String replyto) throws J2InternalException{
        MimeMessage message=null;
                try {
                     message=prepare( server, subject, from ,  to, replyto);
                            message.setContent(text,"text/html");
 
                } catch (AddressException e) {
                    Exceptions.throwInternalException(e);
                } catch (MessagingException e) {
                    Exceptions.throwInternalException(e);
                }
 
 
                try {
                    Transport.send(message);
                } catch (MessagingException e1) {
                    Exceptions.throwInternalException(e1);
                }
    }
 
    /**
     * sends email in plain text with attached file.
     * @param server name or ip of the SMTP server.
     * @param subject Subject.
     * @param text  Body of the email.
     * @param from  'from' address.
     * @param to    'to'  address
     * @param filePath path of the file to attach.
     * @throws J2InternalException  any internal exception.
     *
     * @throws J2InternalException
     */
    public static void sendTextWAttach(String server,String subject, String text, String from , String to,String filePath) throws J2InternalException{
 
        sendTextWAttach( server, subject,  text,  from ,  to, filePath,false);
    }
 
 
/**
 * sends HTML-email with attachement
 * @param server name or ip of the SMTP server.
 * @param subject Subject.
 * @param text Body of the email.
 * @param from 'from' address.
 * @param to 'to'  address.
 * @param filePath path to the file that must be attached.
 * @throws J2InternalException
 */
public static void sendHtmlWAttach(String server,String subject, String text, String from , String to,String filePath) throws J2InternalException{
 
        sendTextWAttach( server, subject,  text,  from ,  to, filePath,true);
    }
 
 
 
    private static void sendTextWAttach(String server,String subject, String text, String from , String to,String filePath,boolean html) throws J2InternalException{
 
        MimeMessage message=null;
 
 
        MimeMultipart mp = new MimeMultipart();
        MimeBodyPart b1 = new MimeBodyPart();
        MimeBodyPart b2 = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(filePath);
        try {
             message=prepare( server, subject, from ,  to, null);
            if(html) {
                b1.setContent(text,"text/html");
            } else {
                b1.setContent(text,"text/plain");
            }
 
            b2.setDataHandler(new DataHandler(fds));
            b2.setFileName(fds.getName());
            mp.addBodyPart(b1);
            mp.addBodyPart(b2);
        } catch (MessagingException e2) {
            Exceptions.throwInternalException(e2);
        }
 
 
                try {
 
                            message.setContent(mp);
 
                } catch (AddressException e) {
                    Exceptions.throwInternalException(e);
                } catch (MessagingException e) {
                    Exceptions.throwInternalException(e);
                }
 
                try {
                    Transport.send(message);
                } catch (MessagingException e1) {
                    Exceptions.throwInternalException(e1);
                }
    }
 
 
 
}