Javamail/Gmail authentication
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Joel_Piazza
Posted On:   Monday, May 28, 2007 09:02 PM

I wrote a program that sends an email using javamail and the gmail smtp server. This code works fine, and it is pasted below: import javax.mail.*; import javax.mail.internet.*; import com.sun.mail.smtp.SMTPTransport; import java.util.*; public class Mailman{ public static void main(String args[]){ AccountManager am = new AccountManager(); int mID = 11523; String initiator = initiator; Vector slots = new Vector (); time t1 = new time(2, 2, 1999, 12, 0); time t2 = new time(2, 2, 1999, 14, 0); time t3 = new time(2, 2, 1999, 9, 0); time t4 = new time(2, 2, 1999, 11, 0); time t5 = new time(2   More>>

I wrote a program that sends an email using javamail and the gmail smtp server. This code works fine, and it is pasted below:



			
import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.smtp.SMTPTransport;

import java.util.*;

public class Mailman{

public static void main(String args[]){
AccountManager am = new AccountManager();
int mID = 11523;
String initiator = initiator;
Vector slots = new Vector ();
time t1 = new time(2, 2, 1999, 12, 0);
time t2 = new time(2, 2, 1999, 14, 0);
time t3 = new time(2, 2, 1999, 9, 0);
time t4 = new time(2, 2, 1999, 11, 0);
time t5 = new time(2, 2, 1999, 13, 45);
time t6 = new time(2, 2, 1999, 15, 45);

Meeting_Slot ms1 = new Meeting_Slot(t1, t2);
Meeting_Slot ms2 = new Meeting_Slot(t3, t4);
Meeting_Slot ms3 = new Meeting_Slot(t5, t6);
slots.add(ms1);
slots.add(ms2);
slots.add(ms3);
String sub = "meeting";
String tit = "design Meeting";
String desc = "Meeting to talk about drexelRadar Design";
Vector atten = new Vector ();
// atten.add("attendee1");
atten.add("attendee2");
Meeting_Request mr = new Meeting_Request(mID, initiator, slots, sub, tit, desc, atten);
sendMail(createMail(mr, am), am);
}

public static String CreateBody(Meeting_Request mr, AccountManager am){
String body = "I am " + am.getUserRealName() + "'s DrexelRadar assistant. He/She requested a meeting with
"
+ "you. He says:
"
+ " "" + mr.getDescription() + ""
"
+ "The time(s) requested by him/her are listed Below:
"
+ "Please respond by filling out this form, using OK or NOTOK as appropriate:
"
+ "
"
+ mr.getListofSlots()
+ "
";

return body;
}

public static Message createMail(Meeting_Request mr, AccountManager am)
{
boolean debug = false;
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtps.host", "smtp.gmail.com ");
props.put("mail.smtps.auth", "true");

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

InternetAddress[] addressTo = new InternetAddress[mr.numAttendees()];
for (int i = 0; i < mr.numAttendees(); i++)
{
try {
addressTo[i] = new InternetAddress(mr.getAttendee(i));
} catch (AddressException e) {
e.printStackTrace();
}
}

// Setting the Subject and Content Type
try {
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject("DRXL_RADAR_" + mr.getMeetingID() + " " + mr.getSubject());
msg.setContent(CreateBody(mr, am), "text/plain");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return msg;

}

public static void sendMail(Message m, AccountManager am){


//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtps.host", "smtp.gmail.com ");
props.put("mail.smtps.auth", "true");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);

SMTPTransport t = null;
try {
t = (SMTPTransport)session.getTransport("smtps");
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
try {
t.connect("smtp.gmail.com", am.getUserName(), am.getPassword());
} catch (MessagingException e) {
e.printStackTrace();
}
try {
t.sendMessage(m, m.getAllRecipients());
t.close();
} catch (SendFailedException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}



}
}



That code works fine. But when i remove main and try to call those methods from another class I get an authentication error. I don't understand why. Its the same exact code and im using the same account name and password. If anyone can shed any light on this i would greatly appreciate it.    <<Less
About | Sitemap | Contact