Netbeans 8.2 - Send email not working after Clean and Build - email

this is making me crazy :) The code works great when i am coding on netbeans (i mean, it sends all emails i want with success) , but, after Clean and Build, it just dont send any email.
zzzzzzzzzzzzzzzzzz = my password
this is my sendig code:
private void envia_email (String email_selecionado) {
String to = email_selecionado;
String from = "xxxxxxxxxx#gmail.com";
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp" );
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory"); //SSL Factory
try {
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, "zzzzzzzzzzz");
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("EUROMILIONS PLAY SOFTWARE");
message.setText(corpo_email.getText());
message.setSentDate(new Date());
Transport.send(message);
//JOptionPane.showMessageDialog(null,"Mensagem enviada com sucesso !");
} catch (MessagingException ex) {
teste.setVisible(false);
JOptionPane.showMessageDialog(this,"Erro: "+ex.toString());
//Logger.getLogger(serial_control.class.getName()).log(Level.SEVERE, null, ex);
}
}
what can be wrong ? why it works on netbeans but not works after Clean and Build.
Thanks

Related

Send email in specific time with Quartz

Hello i would like to ask your help concerning how to send email in quartz executor. That is my code
public class SchedulerJob implements Job{
#EJB
private ParticipationTaskDao participationTaskDao;
public void sendEmail() throws AddressException, MessagingException {
List<ParticipationTask> participantTasks=participationTaskDao.listParticipantTask();
for(ParticipationTask participantTask:participantTasks){
String subject="Task";
String message="You take part to the task "+participant.getTask().getName()+" from "+participant.getTask().getDateStart()+" to "+participant.getTask().getDateEnd()+". Description:"
+ participantTask.getTask().getDescription();
String receiver=participantTask.getUser().getEmail();
System.out.println("Email sent initialisation... ");
try {
final String username="mymail#gmail.com";
final String password="mypassword";
String host = "smtp.gmail.com";
String from = "mymail#gmail.com";
String pass = "mypassword";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "false");
props.put("mail.debug", "true");
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));
MimeMessage message = new MimeMessage(session);
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress(receiver);
message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(message);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
message.saveChanges();
Transport.send(message);
transport.close();
}catch(Exception ex){
System.out.println("<html><head></head><body>");
System.out.println("ERROR: " + ex);
System.out.println("</body></html>");
}
System.out.println(""Email sent successfully);
}
}
#Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
sendEmail();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
When i start my server, i don't see nothing but if i decide to test the below code all things runs correctly
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Quartz runs correctly");
}

Asynchronous email notification in groovy

I have the below code in a groovy class, I want to call this method asynchronously from various other groovy classes.
public void sendNotification(){
//async true
String from = ApplicationConfig.email_From;
String sendTo = ApplicationConfig.email_To;
String host = ApplicationConfig.email_Host;
String subject = ApplicationConfig.email_Subject;
String textToSend = ApplicationConfig.email_Text;
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(sendTo));
message.setSubject(subject);
message.setText(textToSend);
Transport.send(message);
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
So far I couldn't find anything that fits my requirement, there are some plugins in grails, but I'm not using grails.
Just use an ExecutorService
ExecutorService pool = Executors.newFixedThreadPool(2)
def sender = { ->
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", ApplicationConfig.email_Host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(ApplicationConfig.email_From));
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(ApplicationConfig.email_To));
message.setSubject(ApplicationConfig.email_Subject);
message.setText(ApplicationConfig.email_Text);
Transport.send(message);
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
public void sendNotification() {
pool.submit(sender)
}

Javamail Read Body of Message Until EOF

The following code is supposed to send and save messages sent via yahoomail. The sending part works OK but id does not save the sent message. I've been researching and found the following code:
/**
* Read the body of the message until EOF.
*/
public static String collect(BufferedReader in) throws IOException {
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
}
How do I incorporate it in the following code?
public void doSendYahooMail(){
from = txtFrom.getText();
password= new String(txtPassword.getPassword());
to = txtTo.getText();
cc = txtCC.getText();
bcc = txtBCC.getText();
subject = txtSubject.getText();
message_body = jtaMessage.getText();
//String imapHost = "imap.mail.yahoo.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.imap.host", "imap.mail.yahoo.com");
props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.port", "993");
Session session = Session.getInstance(props,null);
try {
// message definition
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
if(!cc.equals("")){
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
if(!bcc.equals("")){
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
message.setSubject(subject);
if(!filename.equals("")){// if a file has been attached...
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message_body);// actual message
Multipart multipart = new MimeMultipart();// create multipart message
// set the text message part
multipart.addBodyPart(messageBodyPart);//add the text message to the multipart
// attachment part
messageBodyPart = new MimeBodyPart();
String attachment = fileAbsolutePath;
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);//add the attachment to the multipart message
// combine text and attachment
message.setContent(multipart);
// send the complete message
Transport.send(message, from, password);
}
else{// if no file has been attached
message.setText(message_body);
Transport.send(message, from, password);
}
JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);
filename = "";//reset filename to null after message is sent
fileAbsolutePath = "";//reset absolute path name to null after message is sent
// save sent message
Store store = session.getStore("imap");
store.connect(from, password);
Folder folder = store.getFolder("Sent");
if(!folder.exists()){
folder.create(Folder.HOLDS_MESSAGES);
Message[] msg = new Message[1];
msg[0] = message;
folder.appendMessages(msg);
}
store.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.toString());
}
}

sending an email from an forms Application (why dosen't it work)

I am trying to learn how to send a simple email with c# visual studio forms Application. (it could also be a console application for all I care). Here is the code I have(I don't see what's wrong with the code, it should work right?):
using System.Net.Mail;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("(here is my email)");
mail.To.Add("(here is my email)");
mail.Subject = "toja";
mail.Body = "ja";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("(here is my email)", "(here is my password)");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
I use the following to send email from C# Forms application and it works.
Maybe you should try adding the smtp.UseDefaultCredentials = false property and set the Credentials property?
// Create our new mail message
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("toAddress");
mailMessage.From = new MailAddress("fromAddress");
mailMessage.Subject = "subject";
mailMessage.Body = "body";
// Set the IsBodyHTML option if it is HTML email
mailMessage.IsBodyHtml = true;
// Enter SMTP client information
SmtpClient smtpClient = new SmtpClient("mail.server.address");
NetworkCredential basicCredential = new NetworkCredential("username", "password");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
smtpClient.Send(mailMessage);
If using Gmail you can find the SMTP server information here https://support.google.com/a/answer/176600?hl=en. It looks like the address is smtp.gmail.com and the port is 465 for SSL or 587 for TLS. I would suggest you try using default port 25 first to make sure your email goes through, then adjust to a different port if you need to.

HttpClient simulation Login

PostMethod post = new PostMethod(
"http://bbs.elecfans.com/member.php?action=login&mod=logging&loginsubmit=yes&loginhash=L55gn");
NameValuePair name = new NameValuePair("username", userName);
NameValuePair pass = new NameValuePair("password", password);
NameValuePair __VIEWSTATE = new NameValuePair(
"__VIEWSTATE",
"loginAddr");
NameValuePair btnLoginx = new NameValuePair("btnLogin.x", "0");
NameValuePair btnLoginy = new NameValuePair("btnLogin.y", "5");
post.setRequestBody(new NameValuePair[] { name, pass, __VIEWSTATE,
btnLoginx, btnLoginy });
try {
client.executeMethod(post);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
post.releaseConnection();
tks
You need to analyze this log on address to find the correct login address and The HttpClient Head to the scrambling of data to.