Anzeige

 JavaMail mit Anhang

#1
Hallo zusammen,

ich probiere mich zur Zeit in Java Mail und will nun eine Mail mit Anhang verschicken. Leider komme ich momentan nicht weiter. Auch Google und Foren durchforsten hat mir nicht geholfen.

Der Code funktioniert ohne den Part mit dem Anhang ohne Probleme.
Jedoch mit dem Anhang bekomme ich folgende Exception:

Code:
Exception in thread "main" javax.mail.AuthenticationFailedException: failed to connect, no password specified?
	at javax.mail.Service.connect(Service.java:329)
	at javax.mail.Service.connect(Service.java:176)
	at javax.mail.Service.connect(Service.java:125)
	at javax.mail.Transport.send0(Transport.java:194)
	at javax.mail.Transport.send(Transport.java:124)
	at example.SendMailWithAttachmentExample.main(SendMailWithAttachmentExample.java:59)
Hier der Code:

Code:
package example;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMailWithAttachment {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Properties props = new Properties();

		String host = "smtp.gmail.com";
		int port = 587;
		String user = "user@googlemail.com";
		String pass = "pass";

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

		Session session = Session.getInstance(props);
		Transport transport = session.getTransport("smtp");
		transport.connect(host, port, user, pass);

		Message message = new MimeMessage(session);
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(
				"ziel@googlemail.com"));

		message.setSubject("Attachment Test");
		message.setText("abcdefg");

		MimeMultipart mimeMultipart = new MimeMultipart();

		MimeBodyPart text = new MimeBodyPart();
		text.setText("abc");
		text.setDisposition(MimeBodyPart.INLINE);

		File file = new File("c:\\bsp.txt");
		MimeBodyPart attachement = new MimeBodyPart();
		attachement.setDataHandler(new DataHandler(new FileDataSource(file)));
		attachement.setFileName(file.getName());
		attachement.setDisposition(MimeBodyPart.ATTACHMENT);
		mimeMultipart.addBodyPart(text);
		mimeMultipart.addBodyPart(attachement);

		message.setContent(mimeMultipart);

		Transport.send(message);

		transport.close();
		System.out.println("DONE");
	}

}
Weiß hier eventuell Jemand eine Lösung? Wahrscheinlich stehe ich nur auf dem Schlauch.


Gruß,
MrTimeey
 
Zuletzt bearbeitet:
#2
Problem gelöst. Es fehlt eine PasswordAuthentification.

Funktionierender Code:

Code:
package mail;

import java.io.File;
import java.net.URL;
import java.util.Properties;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
public class SendMailWithAttachment {
 
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        
    	String host = "smtp.gmail.com";
    	String from = "yourMail@googlemail.com";
    	final String user = "youUser";
    	final String pass = "pass";
    	
    	String ziel = "zielEmail";
    	String filename = "Dateipfad";
    	
    	// SMTP Host setzen
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.port", "" + 587);
		props.setProperty("mail.smtp.starttls.enable", "true");
		

		// Default Session holen, obige Properties und Authentifizierungsdaten setzen
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication(){
				return new PasswordAuthentication(user, pass);
			}});

		// debug mode setzen
		session.setDebug(true);
 
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(ziel));
 
        message.setSubject("Attachment Test");
        message.setText("abcdefg");
 
        MimeMultipart mimeMultipart = new MimeMultipart();
 
        MimeBodyPart text = new MimeBodyPart();
        text.setText("abc");
        text.setDisposition(MimeBodyPart.INLINE);
        
        File file = new File(filename);
        MimeBodyPart attachement = new MimeBodyPart();
        attachement.setDataHandler(new DataHandler(new FileDataSource(file)));
        attachement.setFileName(filename);
        attachement.setDisposition(MimeBodyPart.ATTACHMENT);
        mimeMultipart.addBodyPart(text);
        mimeMultipart.addBodyPart(attachement);
 
        message.setContent(mimeMultipart);
 
        Transport.send(message);
 
        System.out.println("DONE");
    }
 
}
 
Anzeige

Neue Beiträge

Anzeige