Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

send mail with attachement

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi... This is the code that i'm using to send a mail with attachment .. The problem that im facing with this piece of code is that 'send mail' method is not sending the text part... all other details with attachment r going fine ... but the text 'test msg ' is not going... can some plz tell me where i'm going wrong in sending the text..!!!

import java.io.*;
import java.util.*;
import java.net.InetAddress;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.MessagingException;
import javax.mail.Store;
import javax.activation.DataSource.*;


public class TestMail
{
private String smtpServer = "smtpname";
public TestMail(String smtpServer)
{
this.setSmtpServer(smtpServer);
}
public TestMail()
{
this("smtpname");
}

public synchronized void sendMail(String [] addresses, String from, String subject,String text, String attachfile)
throws MessagingException
{
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.debug","false");
MimeBodyPart mbp2 = new MimeBodyPart();
Multipart mp = new MimeMultipart();
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(false);
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress [] intAddresses = new InternetAddress[addresses.length];
for (int i = 0; i < addresses.length; i++)
{
intAddresses[i] = new InternetAddress(addresses[i]);
}
msg.setRecipients(Message.RecipientType.TO,intAddresses );
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setHeader("X-Mailer", "Efficacy Solutions Example Mailer");
msg.setText(text);
javax.activation.DataSource fds = new javax.activation.FileDataSource(attachfile);
mbp2.setDataHandler(new javax.activation.DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
msg.setContent(mp);
msg.saveChanges();
Transport.send(msg);
}

public void setSMTPServer(String server)
{
this.smtpServer = server;
}
public void setSmtpServer(String smtpServer)
{
this.smtpServer = smtpServer;
}
public String getSmtpServer()
{
return smtpServer;
}
public static void main(String arg[])
{
TestMail mail = new TestMail();
String[] address = {"xyz@hotmail.com"};
String attachfile = "C:/filelocation.htm";
try
{
mail.sendMail(address,"emailaddress@email.com","filw with attachment","test Msg",attachfile);
System.out.println("Mail Sent");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Thanxs in advance
 
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Other Java APIs.
 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please can some one see the code and tell me where am i going wrong....!!! coz the text is not going when i send mail with attachment... the attachment goes but the text doesnot..!
 
Rekha Pande
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I searched for related topics in this forum itself and got the solution for my problem. Thanxs every one..!
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well its hard to read and debug ur code but if u want you can take mine.
I am successfully able to send mails and attachments.


/*
package mail;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Random;
import pk_globalfunctions.*;

public class send_mail extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
try{
HttpSession session = request.getSession(true);
String p_no=request.getQueryString();
String dataSourceName = "al_db";
String dbURL = "jdbc dbc:" + dataSourceName;
Connection dbcon = DriverManager.getConnection(dbURL);
Statement stmt1 = dbcon.createStatement();
Statement stmt2 = dbcon.createStatement();


String hostname = request.getParameter("domain");
String to =request.getParameter("txtemail");
String from="admin@superplasticcoats.com";
String subject =request.getParameter("subject"); ;
String body = "see attached file";
String attach ="details.html";

try
{
SmtpAttachment example = new SmtpAttachment();
example.sendMessage(hostname,to,from,subject,body,attach);
}catch(Exception e)
{
System.out.println(e);
}
String url1="/ap/mail/mail_sent.jsp?";
String url=url1+to;
response.sendRedirect(url);

}catch(Exception e2){}
}
}



///

package mail;

import com.jscape.inet.smtp.*;
import com.jscape.inet.mime.*;
import com.jscape.inet.email.*;
import java.io.*;

public class SmtpAttachment extends SmtpAdapter {
public void sendMessage(String hostname, String to, String from, String subject, String body, String attachment) throws SmtpException,
IOException, MimeException {
System.out.println("reached here");
// create new Smtp instance
Smtp smtp = new Smtp(hostname);

// enable debugging ... all output is sent to System.out
smtp.setDebug(true);

// capture SMTP related events
smtp.addSmtpListener(this);

// connect to mail server
smtp.connect();

// create new email message
EmailMessage email = new EmailMessage();

// set recipient address
email.setTo(to);

// set from address
email.setFrom(from);

// set subject
email.setSubject(subject);

// set body
email.setBody(body);

// add file attachment to message
email.addAttachment(new Attachment(new File(attachment)));

// send email message
smtp.send(email);

// disconnect from SMTP server
smtp.disconnect();
}

// capture connect event
public void connected(SmtpConnectedEvent evt) {
System.out.println("Connected to Smtp server: " + evt.getHostname());
}

// capture disconnect event
public void disconnected(SmtpDisconnectedEvent evt) {
System.out.println("Disconnected from Smtp server: " + evt.getHostname());
}

}

*/
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
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