Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 13th, 2003, 07:07 PM
betac betac is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 50 betac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
How do I send mail with Java API?

Hi, Java gurus! Please give your generous help to a helpless newbie!

I wrote a method to send automatic email with Java API. The code was successfully compiled, but does not generate email as it was designed to. I'm not sure if I should write code for SMTP authentication, or if sendmail is not properly configured. Actually, I don't know how to do either.

Thank you for your help!

////////////////////// here is the Java method //////////////////////
public void smail(String myName, String myMail, String recipient, String recipient_mail, String mail_subject, String mail_body)
throws IOException {
String SMTP_HOST = "localhost";
try {
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
Session s = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(myMail);
message.setFrom(from);
String toAddress = recipient_mail;
InternetAddress to = new InternetAddress(toAddress);
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject(mail_subject);
message.setContent(mail_body, "text/html");
Transport.send(message);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}l
//////////////////////////////////////////////////////////////////////////

Reply With Quote
  #2  
Old January 23rd, 2003, 12:37 AM
DC Dalton DC Dalton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: NE Pa.
Posts: 96 DC Dalton User rank is Private First Class (20 - 50 Reputation Level)DC Dalton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 29 m 32 sec
Reputation Power: 8
At first glance I dont see any boo boos, but Javamail is WHOLLY dependent on your hosting companies permissions for mail. MOst servers do not allow outgoing mail of this sort due to the spam problems. They have closed down the open relays BIG TIME. I noticed you are using localhost for your smtp server. Is that what your hosting told you to use...not many host have figured out this trick yet because it does stop spam to a great degree.

Here is a VERY simple (yet effective) mail code I have been using for years. As long as the info is correct & the server allows it, it will work!

public void sendEmail (String userName, String passWord, String emailAddy) {

String host = "localhost";
String to = emailAddy;
String from = "fromMail";
String subject = "subjectMessage";

String messageText = "Some yadda, yadda";
boolean sessionDebug = false;

Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(sessionDebug);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
msg.setText(messageText);
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}

Hope this helps....if it still doesnt work I suggest you talk to your host!
__________________
DC Dalton
DCD Designs
SCJP

Reply With Quote
  #3  
Old January 23rd, 2003, 01:13 AM
betac betac is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 50 betac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Yes, you're right, DC Dalton. It was an SMTP permission issue. I have worked it out anyway. Thanks a lot. I will try out your code

betac

Reply With Quote
  #4  
Old February 19th, 2003, 03:00 PM
mwarford mwarford is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: U.K.
Posts: 15 mwarford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

I am using DC Dalton's email code but I am getting this error message. Can someone enlighten me as to what it means.

Cheers.

DEBUG: setDebug: JavaMail version 1.3
java.lang.NoClassDefFoundError: javax/activation/DataSource
at UI.sendEmail(UI.java:603)
at UI$ButtonHandler.actionPerformed(UI.java:502)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:227)
at java.awt.Component.processMouseEvent(Component.java:5093)
at java.awt.Component.processEvent(Component.java:4890)
at java.awt.Container.processEvent(Container.java:1566)
at java.awt.Component.dispatchEventImpl(Component.java:3598)
at java.awt.Container.dispatchEventImpl(Container.java:1623)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
at java.awt.Container.dispatchEventImpl(Container.java:1609)
at java.awt.Window.dispatchEventImpl(Window.java:1585)
at java.awt.Component.dispatchEvent(Component.java:3439)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

Reply With Quote
  #5  
Old February 19th, 2003, 10:29 PM
DC Dalton DC Dalton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: NE Pa.
Posts: 96 DC Dalton User rank is Private First Class (20 - 50 Reputation Level)DC Dalton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 29 m 32 sec
Reputation Power: 8
Looks like you may not have the activation.jar in your classpath.....it seems that is where it is looking for this DataSource object.....make sure thats right & let me know if it still doesnt work

Reply With Quote
  #6  
Old February 22nd, 2003, 01:14 PM
Lord MJ Lord MJ is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 34 Lord MJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 57 m 2 sec
Reputation Power: 8
Send a message via AIM to Lord MJ Send a message via Yahoo to Lord MJ
DC Dalton,

I noticed that you never passed in the actual text of the message into your method.


That could cause some confusion to some novices, though most Java programmers should be able to figure that out.

Reply With Quote
  #7  
Old February 22nd, 2003, 02:31 PM
DC Dalton DC Dalton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: NE Pa.
Posts: 96 DC Dalton User rank is Private First Class (20 - 50 Reputation Level)DC Dalton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 29 m 32 sec
Reputation Power: 8
The messageText variable in this code is just "some yadda, yadda" which was just there for reference. This variable could either hold a standard message (like this) or text that is brought in from a form....sorry for the confusion there..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > How do I send mail with Java API?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway