Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

What should be the value of 'host' when I send an e-mail?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading about Java Mail API but I have some problem with that. Actually I'm writing a web gallery & I have to use this API so that users can share photos through e-mail & also to send notification in new photos.

...Well I know that the protocols to use are SMTP for sending mail and POP3 or IMAP for receiving mail, and we need to access to a mail server.

But I don't know exactly what can be the value of "host". (String host = "?"

When I run the following program it gives me this exception :

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Unknown SMTP host: smtp.mail.yahoo.com;
nested exception is:
java.net.UnknownHostException: smtp.mail.yahoo.com
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at SimpleSender.send(SimpleSender.java:76)
at SimpleSender.main(SimpleSender.java:26)


(Here is the code I have copied from somewhere, to send an attachment: )

package mail;

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

public class sendwithattachment {
public static void main (String args[])
throws Exception {
String host = "smtp.mail.yahoo.com"; ////////// I don't know what I should put here
String from = "mmaths_happiness@yahoo.com";
String to = "mmaths_happiness@yahoo.com";
String fileAttachment = "C://MainFolder/sth.rtf";

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session =
Session.getInstance(props, null);

// Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment -- This is Maryam!");

// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();

//fill message
messageBodyPart.setText("I did it!");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send( message );
}
}

What can be the problem? & should I add any jar file to my project?

Thanks a ton,

Maryam
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The host value is the host server that provides support for whatever protocol you are using. Yuo have defined a server that appears to be Yahoo. First thing to check: does it exist? Next thing to check: can you access it via SMTP? Its an internet email provider, so I'd guess no for the second question, but its the sort of question you can answer quickest by speaking to the server admin.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic