Cannot access parent's protected method in Java - java

I have this problem:
A java class that contains an inner class. The inner class (Authenticator) extends the javax.mail.Authenticator (note the same name for both class and subclass).
The problem is that I can't access the protected method getPasswordAuthentication unless the inner class has the same name as the extended class (I mean Authenticator extends javax.mail.Authenticator).
If I write private class SMTPAuthenticator extends javax.mail.Authenticator then I can't access anymore the protected getPasswordAuthentication.
See code below:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailWithPasswordAuthentication {
public static void main(String[] args) throws MessagingException {
new MailWithPasswordAuthentication().run();
}
private void run() throws MessagingException {
Message message = new MimeMessage(getSession());
message.addRecipient(RecipientType.TO, new InternetAddress("to#example.com"));
message.addFrom(new InternetAddress[] { new InternetAddress("from#example.com") });
message.setSubject("the subject");
message.setContent("the body", "text/plain");
Transport.send(message);
}
private Session getSession() {
Authenticator authenticator = new Authenticator();
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", "mail.example.com");
properties.setProperty("mail.smtp.port", "25");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "auth-user";
String password = "auth-password";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}
thankx,
A

If you rename your Authenticator class to SMTPAuthenticator, you should also change the line:
Authenticator authenticator = new Authenticator();
to
SMTPAuthenticator authenticator = new SMTPAuthenticator();
The reason you get the error is that only the SMTPAuthenticator class contains the getPasswordAuthentication() method, not the base Authenticator class

If you can access Authenticator extends javax.mail.Authenticatore then you should be able to access class SMTPAuthenticator extends javax.mail.Authenticator. If not then your observation is wrong .

Related

How to use libraries correctly in Netbeans (Java)?

This question is about how to handle libraries in Netbeans, when programming in Java.
I am having a Java project, let's call it ABC. One of its activities is to send an email message. Some of my other Java projects also send email messages, so I decided to create a separate Java project for sending messages. This project is called SendEmail. SendEmail uses external jar files (javax.mail.*). These are included by going to SendEmail's project Properties -> Libraries -> Add JAR. Testing SendEmail works fine: when calling its method sendMail(title, contents) do I receive the email which is sent.
Project ABC uses SendEmail, so I have added this to ABC's library: project Properties -> Libraries -> Add Project. ABC compiles and runs fine until it reaches the point where it wants to send an email: it crashes.
private void informUser(){
//create message title
//create message contents
SendEmail email = new SendEmail();
email.sendMail(title, contents); // <- it crashes here
}
The error information states that it can't find the Authenticator class. This class is in the external jar file which is included in SendEmail's library.
I can only avoid this crash from happening by including the external jar files to ABC's library. This is what I did not expect to be necessary. ABC does not use these external jar files, only SendEmail does.
My question: am I doing something wrong? I assumed that ABC is not using these external jar's so they don't need to be in ABC's library.
In your code don`t have the authenticator part. This code only can use gmail email, you can change smtp server options. My code:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GmailSender extends javax.mail.Authenticator {
private String user;
private String password;
private Session session;
static {
Security.addProvider(new JSSEProvider());
}
public GmailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
And do you need this class:
import java.security.AccessController;
import java.security.Provider;
public class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
For used this code:
class SendEmailTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
try {
GmailSender sender = new GmailSender("from email", "from email password");
//subject, body, sender, to
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
sender.sendMail("your title",
"your content",
"from email",
"to email");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
and run
SendEmailTask sendEmailTask = new SendEmailTask();
sendEmailTask.execute();
UPD1:
Libs:
1. javax.activation
2. javax.mail

javamail isnt sending mail by gmail?

im trying to develop an android mailbox app which would send and receive emails.
but im having issue by sending mail on javamail.
here is the codes;
ComposeMail.Java
package app.mailbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.util.Log;
public class Composemail extends AppCompatActivity {
private String to;
private String subject;
private String message;
private String username;
private String password;
public void sendthemail(View view) {
EditText toarea, subjectarea,messagearea;;
toarea = (EditText) findViewById(R.id.et_to);
subjectarea = (EditText) findViewById(R.id.et_sub);
messagearea = (EditText) findViewById(R.id.et_text);
String to = toarea.getText().toString();
String subject = subjectarea.getText().toString();
String message = messagearea.getText().toString();
try {
GMailSender sender = new GMailSender(username, password);
sender.sendMail(subject, message, username, to);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_composemail);
username = Login.username;
password = Login.password;
}
}
GmailSender.java
package app.mailbox;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new app.mailbox.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
JSSE Provider
package app.mailbox;
import java.security.AccessController;
import java.security.Provider;
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
im new to android development guys, i have no idea about why it isnt running, i also tryed debugging too but didnt find out anything.
https://www.tutorialspoint.com/javamail_api/javamail_api_gmail_smtp_server.htm
also can i use this tutorial for android javamail app? or its not for android?
and i need a solution about how i can add a google autherization i mean a checking system which will check if given email and password is exist as gmail account? and i want to do that at login screen?

Send html email from file with variables from java - java

I want to send an email with html markup text.
I want that my code gets the message from a file on my pc and I want to be able to use variables I have in my project.
so that my message comes up like: Hello [username]
and that [username] is a variable.
package com.email;
import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail extends javax.mail.Authenticator {
private String _user;
private String _pass;
private String[] _to = new String[1];
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public SendMail() {
_host = "smtp.live.com"; // default smtp server
_port = "587"; // default smtp port
_sport = "587"; // default socketfactory port
_user = "user#hotmail.com"; // username
_pass = "password"; // password
_from = "user#hotmail.com"; // email sent from
_subject = "Welcome to Ravenous!"; // email subject
_body = "<h2 style='font-style: normal;font-weight: 700;Margin-bottom: 0;Margin-top: 0;font-size: 24px;line-height: 32px;font-family: Open Sans,sans-serif;color: #44a8c7;text-align: center'>Welcome to Ravenous!</h2><p style='font-style: normal;font-weight: 400;Margin-bottom: 0;Margin-top: 16px;font-size: 15px;line-height: 24px;font-family: Open Sans,sans-serif;color: #60666d;text-align: center'>Hello, we hope that you enjoy your stay on Ravenous.</p>"; // email body
_to[0] = "";
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public SendMail(String to) {
this();
_user = "user#hotmail.com";
_pass = "password";
_to[0] = to;
}
public boolean send() throws Exception {
Properties props = _setProperties();
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(_body, "text/html; charset=utf-8");
_multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(_multipart);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
#Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.starttls.enable", "true");
return props;
}
// the getters and setters
public String getBody() {
return _body;
}
public void setBody(String _body) {
this._body = _body;
}
public String[] getTo() {
return _to;
}
public void setTo(String[] _to) {
this._to = _to;
}
public String getFrom() {
return _from;
}
public void setFrom(String _from) {
this._from = _from;
}
public String getSubject() {
return _subject;
}
public void setSubject(String _subject) {
this._subject = _subject;
}
// more of the getters and setters …..
}
So how would I get my html message from an html file and send it with some variables?
This is what my template file looks like:
package com.email;
import java.io.*;
import java.util.*;
import com.world.entity.impl.player.Player;
import freemarker.template.*;
public class Template {
public static String body;
public static void getTemplate() throws Exception {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
cfg.setDirectoryForTemplateLoading(new File("./data/templates/"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Map<String, Object> root = new HashMap<String, Object>();
root.put("userName", SendMail._username);
Map<String, String> latest = new HashMap<String, String>();
root.put("latestProduct", latest);
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");
freemarker.template.Template temp = cfg.getTemplate("Welcome.ftl");
Writer out = new OutputStreamWriter(System.out);
body = out.toString();
temp.process(root, out);
}
}
Use a templating engine for this, I personally did this with freemarker.
It allows you to store a template alongside your program which you fill by using variables passed from your code.
Map root = new HashMap();
root.put("name", "John Doe");
...
Template temp = cfg.getTemplate("mymailtemplate.ftl");
...
In the template you simply write something like this:
<h1>Welcome ${name}!</h1>
There is a good example on the freemarker page

How can I create run time entry for email and password in following code?

Following is the code of Main.java file.
package com.app.mail1;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
EditText to, from, message, subject;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
GMailSender sender = new GMailSender("rockstarjamunjuice#gmail.com", "jamunjuice");
sender.sendMail("This is Subject",
"This is Body",
"user#gmail.com",
"user#yahoo.com");
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
});
}
}
Here is the code for GMailSender.java file
package com.app.mail1;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.provider.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
Can I set the username and password at run time?
Can I make String to Editable?
Please help me if I can edit this code and set sender's email id and password at run time?
Howto convert String to Editable
Howto change username/password:
Add this to class:
public class Main extends Activity {
private static String password;
private static String username;
public static void setUsername(String user){
username = user;
}
public static void setPassword(String pass){
password = pass;
}
// ...................................
GMailSender sender = new GMailSender(username, password);
}
Then, you can change pass/name:
public class SomeClass {
Main.setUsername("SomeUser");
Main.setPassword("StrongPassword");
}
Or you can use Intent for transfer data (username/password) in Activity

Mail.java:18: error: cannot find symbol

import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public String to;
public String subject;
public String text;
SendMail(String to, String subject, String text){
this.to = to;
this.subject = subject;
this.text = text;
}
public void send() throws NoSuchProviderException, AddressException{
try
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props=new Properties();
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.host","mail.epro-tech.com");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
props.put("mail.debug","true");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
Session session=Session.getDefaultInstance(props,new GJMailAuthenticator());
session.setDebug(true);
Transport transport=session.getTransport();
InternetAddress addressFrom=new InternetAddress("itopstest#epro-tech.com");
MimeMessage message=new MimeMessage(session);
message.setSender(addressFrom);
message.setSubject(subject);
message.setContent(text,"text/html");
InternetAddress addressTo=new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO,addressTo);
transport.connect();
Transport.send(message);
transport.close();
System.out.println("DONE");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class GJMailAuthenticator extends javax.mail.Authenticator{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("itopstest#epro-tech.com","Ops#890T");
}
}
public class Mail extends SendMail {
public static void main(String[] args) {
String to = "noreply#eprocorp.com";
String subject = "Test";
String message = "A test message";
SendMail SendMail = new SendMail(to, subject, message);
try
{
SendMail.send();
}
catch (Exception e)
{
//
}
}
}
receiving the error
Mail.java:18: error: cannot find symbol
SendMail SendMail = new SendMail(to, subject, message);
^
symbol: class SendMail
location: class Mail
Mail.java:18: error: cannot find symbol
SendMail SendMail = new SendMail(to, subject, message);
^
symbol: class SendMail
location: class Mail
2 errors
can any one plz suggest me how to rectify this
I can see there are problems.
Mail class is extending SendMail class which do not have default cunstructor. so either create a default cunstructor in SendMail class or create a parameterize cunstructor in Mail class
SendMail.send(); here i guess compiler is trying to access static method of SendMail class. create your object like SendMail sendMail = new SendMail(to, subject, message); and access send method like sendMail.send();
Give proper package name to both your classes because your class has common names which may be found in jars.
code already containd a constructor
SendMail(String to, String subject, String text)
{
this.to = to;
this.subject = subject;
this.text = text;
}
even at extends in the class Mail i can see the errors

Categories

Resources