You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Paul Hsu <hs...@verizon.net> on 2003/03/09 06:29:00 UTC

Java mail

Hi,

I try to develope a java mail. I have a problem about how to authenticate email sender against email server. Since the email server ask for authentication if you want to send a email through that server because of spam issue. I got 
javax.mail.MessagingException==553 Authentication is required to send mail as <hs...@verizon.net>
error every time. The following is my Mailer class. I wonder if any one know what is wrong with my code. Any help will be appreciated.

package com.smartequip.email;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.*;
import java.io.*;
import com.smartequip.common.AppBase;

public class Mailer extends AppBase
{

    public static String TEXTPLAIN = "1";
    public static String TEXTHTML = "2";
    public static String EDI = "3";

    private String smtpHost=null;
    private String subject=null;
    private String from=null;
    private Vector toV = new Vector();
    private Vector ccV = new Vector();
    private Vector attachedV = new Vector();
    private String textMessage=null;
    private String message_type = "1";
    private String userName="";
    private String password="";
    private Auth auth;
    private Session session;

    public Mailer()
    {
    }

    public Mailer(String smtpHost)
    {
        this.smtpHost = smtpHost;
    }

    public void setUserName(String s)
    {
        this.userName = s;
    }

    public void setPassword(String s)
    {
        this.password = s;
    }

    public void setSmtpHost(String smtpHost)
    {
        this.smtpHost = smtpHost;
    }

    public void setFrom(String from)
    {
        this.from = from;
    }

    public void addTo(String to)
    {
        toV.add(to);
    }

    public void addCc(String cc)
    {
        ccV.add(cc);
    }
    public void setSubject(String subject)
    {
        this.subject = subject;
    }

    public void addAttached(String attached)
    {
        attachedV.add(attached);
    }

    public void setTextMessage(String message)
    {
        this.textMessage = message;
    }


    public void setMessageType(String t)
    {
        this.message_type = t;
    }

    public void send() throws Exception
    {

        Message msg = prepareHeader();
        if(message_type.equals(TEXTPLAIN))
            msg.setContent(textMessage, "text/plain");
        if(message_type.equals(TEXTHTML))
            msg.setContent(textMessage, "text/html");
        sendMesg(msg);
    }


    private void sendMesg(Message msg) throws Exception
    {
        msg.saveChanges(); // implicit with send()
        Transport transport = session.getTransport("smtp");
        transport.connect(smtpHost, userName, password);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }


    private Message prepareHeader() throws Exception
    {
        Properties props = new Properties();
        if(smtpHost == null)
            throw new Exception("No SMTP Host specified.");
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.user", userName);
        props.put("password", password);

        session = Session.getDefaultInstance(props, null);
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        // set from
        if(from == null)
            throw new Exception("No Sender specified.");
        InternetAddress addr = new InternetAddress(from);
        msg.setFrom(addr);
        // set to
        if(toV.size() == 0)
            throw new Exception("No Recipients specified.");
        for (int i = 0; i < toV.size(); i++)
        {
            InternetAddress addrt = new InternetAddress((String)toV.get(i));
            msg.addRecipient(Message.RecipientType.TO, addrt);
        }

        // set cc
        for (int i = 0; i < ccV.size(); i++)
        {
            InternetAddress addrt = new InternetAddress((String)ccV.get(i));
            msg.addRecipient(Message.RecipientType.CC, addrt);
        }

        // set subject
        if(subject == null)
            throw new Exception("No Subject specified.");
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        return msg;

    }

} // Mailer


thanks

Paul 

Re: Java mail

Posted by Dan Tran <da...@hotmail.com>.
You need to execute the below code before sending message
to setup System authentication

        Properties props = System.getProperties();
        if ( authenticationRequired)  <--- like if useriD.length != 0
            props.put("mail.smtp.auth", "true");
        else
            props.put("mail.smtp.auth", "false");

-Dan


----- Original Message -----
From: "Paul Hsu" <hs...@verizon.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Cc: "huikuo hsu" <hs...@verizon.net>
Sent: Saturday, March 08, 2003 9:29 PM
Subject: Java mail


Hi,

I try to develope a java mail. I have a problem about how to authenticate
email sender against email server. Since the email server ask for
authentication if you want to send a email through that server because of
spam issue. I got
javax.mail.MessagingException==553 Authentication is required to send mail
as <hs...@verizon.net>
error every time. The following is my Mailer class. I wonder if any one know
what is wrong with my code. Any help will be appreciated.

package com.smartequip.email;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.*;
import java.io.*;
import com.smartequip.common.AppBase;

public class Mailer extends AppBase
{

    public static String TEXTPLAIN = "1";
    public static String TEXTHTML = "2";
    public static String EDI = "3";

    private String smtpHost=null;
    private String subject=null;
    private String from=null;
    private Vector toV = new Vector();
    private Vector ccV = new Vector();
    private Vector attachedV = new Vector();
    private String textMessage=null;
    private String message_type = "1";
    private String userName="";
    private String password="";
    private Auth auth;
    private Session session;

    public Mailer()
    {
    }

    public Mailer(String smtpHost)
    {
        this.smtpHost = smtpHost;
    }

    public void setUserName(String s)
    {
        this.userName = s;
    }

    public void setPassword(String s)
    {
        this.password = s;
    }

    public void setSmtpHost(String smtpHost)
    {
        this.smtpHost = smtpHost;
    }

    public void setFrom(String from)
    {
        this.from = from;
    }

    public void addTo(String to)
    {
        toV.add(to);
    }

    public void addCc(String cc)
    {
        ccV.add(cc);
    }
    public void setSubject(String subject)
    {
        this.subject = subject;
    }

    public void addAttached(String attached)
    {
        attachedV.add(attached);
    }

    public void setTextMessage(String message)
    {
        this.textMessage = message;
    }


    public void setMessageType(String t)
    {
        this.message_type = t;
    }

    public void send() throws Exception
    {

        Message msg = prepareHeader();
        if(message_type.equals(TEXTPLAIN))
            msg.setContent(textMessage, "text/plain");
        if(message_type.equals(TEXTHTML))
            msg.setContent(textMessage, "text/html");
        sendMesg(msg);
    }


    private void sendMesg(Message msg) throws Exception
    {
        msg.saveChanges(); // implicit with send()
        Transport transport = session.getTransport("smtp");
        transport.connect(smtpHost, userName, password);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    }


    private Message prepareHeader() throws Exception
    {
        Properties props = new Properties();
        if(smtpHost == null)
            throw new Exception("No SMTP Host specified.");
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.user", userName);
        props.put("password", password);

        session = Session.getDefaultInstance(props, null);
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        // set from
        if(from == null)
            throw new Exception("No Sender specified.");
        InternetAddress addr = new InternetAddress(from);
        msg.setFrom(addr);
        // set to
        if(toV.size() == 0)
            throw new Exception("No Recipients specified.");
        for (int i = 0; i < toV.size(); i++)
        {
            InternetAddress addrt = new InternetAddress((String)toV.get(i));
            msg.addRecipient(Message.RecipientType.TO, addrt);
        }

        // set cc
        for (int i = 0; i < ccV.size(); i++)
        {
            InternetAddress addrt = new InternetAddress((String)ccV.get(i));
            msg.addRecipient(Message.RecipientType.CC, addrt);
        }

        // set subject
        if(subject == null)
            throw new Exception("No Subject specified.");
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        return msg;

    }

} // Mailer


thanks

Paul

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org