Having issues determining how to use the following code to add attachements from the SD card of the Ethernet Shield. I am using a Mega 2560 with Ethernet Shield. The code works great for sending the e-mail thru my current setup but need to attach small 10K TXT or CSV file. Below is the code I am using. Any suggestions would be greatly appreciated.
#include <SPI.h>
#include <Ethernet.h>
#include "Smtp_Service.h"
const unsigned int SMTP_PORT = 587;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = {192, 168, 1, 20};
byte gateway[] = {192, 168, 1, 1};
byte smtp_server[] = { 193, 0, 0, 0};
SmtpService smtp_service(smtp_server, SMTP_PORT);
char incString[250];
String domain = "jayconsystems.com";
String login = "bG9naW4=";
String password = "cGFzc3dvcmQ=";
Email email;
void setup()
{
Ethernet.begin(mac, my_ip, gateway);
Serial.begin(9600);
Serial.setTimeout(500000);
delay(1000);
email.setDomain(domain);
email.setLogin(login);
email.setPassword(password);
email.setFrom("arduino@me.com");
email.setTo("username@domain.com");
email.setCc("");
email.setSubject("Jayconsystems is amazing!");
email.setBody("It really is =)");
}
void loop()
{
if (Serial.available() > 0) {
byte inByte = Serial.read();
if (inByte == 'S')
{
smtp_service.send_email(email);
}
delay(1000);
}
}
Here is the Smtp_Service.h library code:
#ifndef _SMTP_SERVICE__H
#define _SMTP_SERVICE__H
#include "email.h"
class SmtpService
{
byte* _smtp_server;
unsigned int _port;
void read_response(Client& client)
{
delay(50);
while(client.available())
{
const char c = client.read();
Serial.print(c);
}
}
void send_line(Client& client, String line)
{
const unsigned int MAX_LINE = 256;
char buffer[MAX_LINE];
line.toCharArray(buffer, MAX_LINE);
Serial.println(buffer);
client.println(buffer);
read_response(client);
}
public:
SmtpService(
byte* smtp_server,
const unsigned int port) : _smtp_server(smtp_server),
_port(port) {}
void send_email (const Email& email)
{
EthernetClient client;
Serial.print("Connecting...");
if(!client.connect(_smtp_server, _port))
{
Serial.println("connection failed.");
}
else
{
Serial.println("Connected.");
read_response(client);
send_line(client, String("ehlo ") + email.getDomain());
send_line(client, String("AUTH LOGIN"));
send_line(client, email.getLogin());
send_line(client, email.getPassword());
send_line(client, String("mail from: <") + email.getFrom() + String(">"));
send_line(client, String("rcpt to: <") + email.getTo() + String(">"));
send_line(client, String("rcpt to: <") + email.getCc() + String(">"));
send_line(client, String("data"));
send_line(client, String("from: ") + email.getFrom());
send_line(client, String("to: ") + email.getTo());
send_line(client, String("subject: ") + email.getSubject());
send_line(client, String(""));
send_line(client, email.getBody());
send_line(client, String("."));
send_line(client, String("quit"));
client.println("Disconnecting.");
client.stop();
}
}
};
#endif
And here is the email.h library code
#ifndef _EMAIL__H
#define _EMAIL__H
class Email
{
String _domain, _login, _password, _from, _to, _cc, _subject, _body;
public:
Email(
const String& domain = "",
const String& login = "",
const String& password = "",
const String& from = "",
const String& to = "",
const String& cc = "",
const String& subject = "",
const String& body = ""
) : _domain(domain), _login(login), _password(password), _from(from), _to(to), _cc(cc), _subject(subject), _body(body) {}
const String& getDomain() const { return _domain; }
const String& getLogin() const { return _login; }
const String& getPassword() const { return _password; }
const String& getFrom() const { return _from; }
const String& getTo() const { return _to; }
const String& getCc() const { return _cc; }
const String& getSubject() const { return _subject; }
const String& getBody() const { return _body; }
void setDomain( const String domain) { _domain = domain; }
void setLogin( const String login) { _login = login; }
void setPassword( const String password) { _password= password; }
void setFrom( const String from) { _from = from; }
void setTo( const String to) { _to = to; }
void setCc( const String cc) { _cc = cc; }
void setSubject( const String subject) { _subject = subject; }
void setBody( const String body) { _body = body; }
};
#endif