|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ////////////////////////////////////////////////////////////////////////// |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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) |
|
#5
|
|||
|
|||
|
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
![]() |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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..
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > How do I send mail with Java API? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|