EMail über Exchange versenden 5.7.1

AlexD1979

Erfahrenes Mitglied
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:
/*
 * 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		
Boolean auth = ConfigUtils.getBoolean("smtp_auth");		
//Das wird ebenfalls durchlaufen		
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;
	}


	/**
	 * 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);
				}
	}



}
 
Zurück