Frage

Ich habe Java-Code zum Senden von E-Mails geschrieben, der eine Ausnahme darstellt :wenn ich Portnummer 465 benutze

kom.Sonne.Mail.SMTP.SMTPSendFailedException:530-5.5.1 Authentifizierung erforderlich.Erfahren Sie mehr unter 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 l1sm2119061stk.54

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

String USERNAME = username; 
String PASSWORD = password; 


Properties props = new Properties(); 
props.put("mail.smtp.host", smtpHost); 
if(smtpPort != null){
    int SMTP_PORT = Integer.parseInt(smtpPort); 
    props.put("mail.smtp.port", SMTP_PORT); 
}
props.put("mail.from",from);
props.put("mail.smtp.starttls.enable", "true"); 
if( auth == 1){
    props.put("mail.smtp.auth", "true"); 
}
props.put("mail.debug", "true"); 

Session session = Session.getInstance(props, new Authenticator() { 
        @Override 
        protected PasswordAuthentication getPasswordAuthentication() { 
            return new PasswordAuthentication(USERNAME, PASSWORD); 
        } 
}); 

MimeMessage msg = new MimeMessage(session); 
msg.setFrom(); 
msg.setRecipients(Message.RecipientType.TO, to); 
msg.setSubject(subject); 
msg.setSentDate(new Date());

Multipart content = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();

bodyPart.setText(htmlBody, "UTF-8");
bodyPart.setContent(htmlBody, "text/html");

content.addBodyPart(bodyPart);

if ( attachmentPath != null ){
    String[] a = attachmentPath.split(",");

    for (int i = 0; i < a.length; i++) { 
            if( a[i] != null )
        {
            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.attachFile(a[i].trim());
            content.addBodyPart(attachmentPart);
        }
    }
}


msg.setContent(content);

Transport.send(msg); 
return true
War es hilfreich?

Lösung

Code ist selbsterklärend, dies ist ein funktionierender Code, den ich in einem Projekt verwende habe.Hoffe das hilft dir raus.Viel Glück.

generasacodicetagpre.

Andere Tipps

wenn ich Portnummer -1

verwende

kaum überraschend.-1 ist keine gültige Portnummer.Was passiert, wenn Sie eine -Recor-Portnummer verwenden?

Wenn ich Portnummer 465

verwende

das ist auch kein Standard-SMTP-Anschluss.

Was passiert, wenn Sie die Richtige -Portnummer verwenden?

Verwenden Sie den folgenden Code, der E-Mails authentifiziert und an den Google Mail-Server sendet javax.mail.Achten Sie auf die Firewall und den von Ihnen ausgewählten Host-Port.

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

final class MailClient
{
    private class SMTPAuthenticator extends Authenticator
    {
        private PasswordAuthentication authentication;

        public SMTPAuthenticator(String login, String password)
        {
             authentication = new PasswordAuthentication(login, password);
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
             return authentication;
        }
    }

    public void mail()
    {
        try
        {
            String from = "xyz.com";
            String to = "abc.com";
            String subject = "Your Subject.";
            String message = "Message Text.";
            String login = "xyz.com";
            String password = "password";

            Properties props = new Properties();
            props.setProperty("mail.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.starttls.enable", "true");

            Authenticator auth = new SMTPAuthenticator(login, password);

            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);

           try
           {
                msg.setText(message);
                msg.setSubject(subject);
                msg.setFrom(new InternetAddress(from));
                msg.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));
                Transport.send(msg);
           }
           catch (MessagingException ex)
           {
                Logger.getLogger(MailClient.class.getName()).
                log(Level.SEVERE, null, ex);
           }
        }
    }
}

final public class Main
{
    public static void main(String...args)
    {
        new MailClient().mail();
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top