RabbitMQ - SimpleAmqpClient - I am trying to send headers along with my message but the headers are not being sent; what am I doing wrong? - c++11

I am using the following: https://github.com/alanxz/SimpleAmqpClient
I am trying to send headers along with my message but the headers are not being sent; what am I doing wrong?
Here is how my code looks like. I have a configuration object with some basic configuration values.
auto channel = AmqpClient::Channel::Create("localhost", 5672, configuration.UserName, configuration.Password, configuration.VirtualHost, 131072);
channel->DeclareQueue(configuration.QueueName, false, true, false, true);
auto messageBody = "simple json string message nothing fancy"
auto message = AmqpClient::BasicMessage::Create(messageBody);
message->DeliveryMode(AmqpClient::BasicMessage::delivery_mode_t::dm_nonpersistent);
message->ContentType("application/json");
message->Type("XYZRequest");
message->AppId("a guid");
auto headerTable = message->HeaderTable();
headersTable.insert(std::pair<string, string>("Key-1", "value-1"));
headersTable.insert(std::pair<string, string>("Key-2", "value-2"));
channel->BasicPublish(std::string(), configuration.ScoreQueueName, message);
This sends the message to the queue and I can see all the details (AppID, Type, Message Body, etc.) on the RabbitMq management portal except the headers.
What am I missing? Is it some configuration or what is it?
I would appreciate if some one can give me a link to a basic tutorial on how to send headers.
I am stuck. Please help.

message->HeaderTable() does not return a reference to the headers, it returns a copy of it.
To set the headers you must construct the headersTable first, then use message->HeadersTable(headersTable).
Table headersTable;
headersTable.insert(std::pair<string, string>("Key-1", "value-1"));
headersTable.insert(std::pair<string, string>("Key-2", "value-2"));
message->HeadersTable(headersTable);

Related

JMeter: how to send formatted text via SMPT sampler

Is there a way to send a formatted text with links via JMeter SMTP sampler?
Scenario:
I need to send an email, where one of the words e.g. My "Instagram" will have a link to my instagram page.
Option 1:
Create such email in Gmail, send it to myself, then download it as .eml file and send use "Send .eml" option in SMTP sampler.
However, my issue is that these links should be changed and lead to different instagram pages with each new email sent, thus I need to pass it as a variable from CSV file. This seems to be impossible to achieve with .eml file as it needs to be modified before each request. Unless there is a way?
Option 2 (preferred):
Somehow I need to format text in "Message body" of SMTP sampler. I've tried to copy/paste the same style and tags from "Original" .eml file, but it is always sent as a plain text and Gmail won't format it on client side.
Here is an example of RAW Gmail text with formatted link which I've tried to use in "Message" text box of SMTP Sampler:
Visit my account #dummyaccount<https://l.instagram.com/?u=https%3A%2F%2Ftaplink.> for more info.
Expecting to see the following in the email:
Visit my account #dummyaccount - where #dummyaccount is a hyperlink
Actual:
Visit my account #dummyaccounthttps://l.instagram.com/?u=https%3A%2F%2Ftaplink. for more info.
Any suggestion will be greatly appreciated.
I don't think you can customize the SMTP Sampler messages this way. You will need to either compose the message in an email application, save it to .eml file and send this .eml or if you want to build the message content dynamically you can use JSR223 Sampler and Groovy language for sending your message.
Example code:
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-server-here");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
def session = javax.mail.Session.getInstance(props, new CredentialsAuthentication() as javax.mail.Authenticator)
def msg = new javax.mail.internet.MimeMessage(session)
def from = new javax.mail.internet.InternetAddress("your-email-address-here", "your first and last name");
msg.setFrom(from);
def toAddress = new javax.mail.internet.InternetAddress("your-recipient-here ");
msg.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
msg.setSubject("Test");
msg.setContent("<html>\n" +
"<body>\n" +
"\n" +
"<a href=\"http://link-to-your-instagram-profile\">\n" +
"Link to my Instagram</a>\n" +
"\n" +
"</body>\n" +
"</html>", "text/html")
javax.mail.Transport.send(msg);
class CredentialsAuthentication extends javax.mail.Authenticator {
#Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication("your-username#most-probably-with-email.com", "your-password-here")
}
}

Get message content from mime message?

I have a java spring integration project that is receving emails through the below code:
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext(
"/integration/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
org.springframework.messaging.Message<MimeMailMessage> received =
(org.springframework.messaging.Message<MimeMailMessage>) message;
log.info("content" + message);
List<String> sentences = null;
try {
} catch (Exception e) {
}
I get the email, and I can get the subject, but I can never actually extract the message body. How do I do this?
Thank you!
You have to use this option on the channel adapter:
simple-content="true"
See its description:
When 'true', messages produced by the source will be rendered by 'MimeMessage.getContent()'
which is usually just the body for a simple text email. When false (default) the content
is rendered by the 'getContent()' method on the actual message returned by the underlying
javamail implementation.
For example, an IMAP message is rendered with some message headers.
This attribute is provided so that users can enable the previous behavior, which just
rendered the body.
But still it is doubtful, since I see in case of GMail message it is never simple. The content is a MimeMultipart and we need to read its parts to get access to the real body.
So, this is how you should change your code as well:
log.info("content" + ((MimeMultipart) ((MimeMessage) message.getPayload()).getContent()).getBodyPart(0).getContent());

Request-response pattern using Spring amqp library

everyone. I have an HTTP API for posting messages in a RabbitMQ broker and I need to implement the request-response pattern in order to receive the responses from the server. So I am something like a bridge between the clients and the server. I push the messages to the broker with specific routing-key and there is a Consumer for that messages, which is publishing back massages as response and my API must consume the response for every request. So the diagram is something like this:
So what I do is the following- For every HTTP session I create a temporary responseQueue(which is bound to the default exchange, with routing key the name of that queue), after that I set the replyTo header of the message to be the name of the response queue(where I will wait for the response) and also set the template replyQueue to that queue. Here is my code:
public void sendMessage(AbstractEvent objectToSend, final String routingKey) {
final Queue responseQueue = rabbitAdmin.declareQueue();
byte[] messageAsBytes = null;
try {
messageAsBytes = new ObjectMapper().writeValueAsBytes(objectToSend);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
MessageProperties properties = new MessageProperties();
properties.setHeader("ContentType", MessageBodyFormat.JSON);
properties.setReplyTo(responseQueue.getName());
requestTemplate.setReplyQueue(responseQueue);
Message message = new Message(messageAsBytes, properties);
Message receivedMessage = (Message)requestTemplate.convertSendAndReceive(routingKey, message);
}
So what is the problem: The message is sent, after that it is consumed by the Consumer and its response is correctly sent to the right queue, but for some reason it is not taken back in the convertSendAndReceived method and after the set timeout my receivedMessage is null. So I tried to do several things- I started to inspect the spring code(by the way it's a real nightmare to do that) and saw that is I don't declare the response queue it creates a temporal for me, and the replyTo header is set to the name of the queue(the same what I do). The result was the same- the receivedMessage is still null. After that I decided to use another template which uses the default exchange, because the responseQueue is bound to that exchange:
requestTemplate.send(routingKey, message);
Message receivedMessage = receivingTemplate.receive(responseQueue.getName());
The result was the same- the responseMessage is still null.
The versions of the amqp and rabbit are respectively 1.2.1 and 1.2.0. So I am sure that I miss something, but I don't know what is it, so if someone can help me I would be extremely grateful.
1> It's strange that RabbitTemplate uses doSendAndReceiveWithFixed if you provide the requestTemplate.setReplyQueue(responseQueue). Looks like it is false in your explanation.
2> To make it worked with fixed ReplyQueue you should configure a reply ListenerContainer:
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory);
container.setQueues(responseQueue);
container.setMessageListener(requestTemplate);
3> But the most important part here is around correlation. The RabbitTemplate.sendAndReceive populates correlationId message property, but the consumer side has to get deal with it, too: it's not enough just to send reply to the responseQueue, the reply message should has the same correlationId property. See here: how to send response from consumer to producer to the particular request using Spring AMQP?
BTW there is no reason to populate the Message manually: You can just simply support Jackson2JsonMessageConverter to the RabbitTemplate and it will convert your objectToSend to the JSON bytes automatically with appropriate headers.

Javamail Content-Type

I'm trying to send an Email via javamail adding embedded Images and an Outlook Invation which should be displayed showing up the buttons to accept/decline .... To make the buttons show up, i have to set the Content-Type of the MimeMultipart to related.
MimeMultipart multipart = new MimeMultipart("related");
Once this is done, the embedded Pictures wont show up again.
To Display the embedded images it should be alternative.
I have send myself a Mail from Outlook containing an Image as well as a invation to a meeting.
When I check the header there, it says:
Content-Type: multipart/related;
boundary="_005_CCF531A072806F489D02A6DD2CF322A01D88EEA2de35s004hst42wp_";
type="multipart/alternative"
I have already tried to set the Content-Type adding Parameters (type, charset ...) but it seems like their just getting ignored when sending the message. Here is the Method which I am calling to create the Message Part:
private Message generateMessage() throws Exception
{
Configuration conf = Configuration.getInstance();
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", conf.getProperty(Configuration.MAIL_SERVER));
// Get session
Session session = Session.getDefaultInstance(props, null);
// Create the message
Message message = new MimeMessage(session);
// Fill its headers
message.setSubject(this.subject);
message.setFrom(new InternetAddress(fromEmail));
Address[] replyTo = new Address[]{new InternetAddress(replyEmail)};
message.setReplyTo(replyTo);
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
multipart.addBodyPart(messageBodyPart);
// ADD IMAGES
multipart = attachImages(multipart);
// ADD CALENDAR-FILE
multipart = attachCalendarFile(multipart);
// Set Content-Type
ContentType ct = new ContentType(message.getContentType());
ct.setParameter("charset", "utf-8");
ct.setParameter("type", "multipart/alternative");
// Set Multipart-Content to Message including the Content-Type
message.setContent(multipart, ct.toString());
return message;
}
The output of message.writeTo(System.out); is the following:
From: mail#sender.com
Reply-To: noreply#recipient.com
Message-ID: <1150322817.1.1397725164700.JavaMail.User#MacBooks-mailserver.local>
Subject: test
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_Part_0_72769750.1397725164655"
------=_Part_0_72769750.1397725164655
Is there any chance to make it work with embedded images AND invations?
Thanks in advance!
There's three types of multiparts, each used for a different purpose. In some cases you might need to use all three. For example, a message with a calendar attachment, html content using images, and equivalent plain text content, might have a structure such as this:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/jpeg
text/calendar
Google search led me here, so for future searchers:
#bill-shannon is of course correct. However, please save yourself the trouble of figuring out the low level Mime RFC stuff, and just use something like Simple Java Mail (open source) which figures out which structure suits best based on the input you provide. You can interactively explore all the possible structures Simple Java Mail can come up with. Disclaimer: I maintain this library (but I actually came across this SO question in my research into including voting buttons in emails for Outlook :) )
In your case you would use the following. First produce a Calendar content string, using something like ical4j:
Calendar icsCalendar = new Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(Version.VERSION_2_0);
(..) // add attendees, organizer, end/start date and whatever else you need
// Produce calendar string
ByteArrayOutputStream bOutStream = new ByteArrayOutputStream();
new CalendarOutputter().output(icsCalendar, bOutStream);
String yourICalEventString = bOutStream.toString("UTF-8")
Then use Simple Java Mail's email builder to set the Calendar content alongside everything else such as images:
Email email = EmailBuilder
.startingBlank()
.withCalendarText(CalendarMethod.REQUEST, yourICalEventString)
.everythingElseLikeImagesHeadersAttachmentsEtcEtc()
.buildEmail();
Then send using the mailer builder:
MailerBuilder
.withSMTPServer("server", 25, "username", "password")
.buildMailer()
.sendMail(email);
Then depending on whether you provided text, html attachments or other things, Simple Java Mail will select the appropriate Mime structure with mixed/related/alternative parts.

Forward email using EWS managed api keeping the headers

I am looking for a sample code on how to forward an existing email message (one that is already in my inbox) using the managed api.
When forwarded is there some way to keep a message original headers while forwarding it?
for example someone sent an email to me -i would like that ews will forward it to another recipient without changing the headers (original receive time from ,bcc etc...).
Given an EmailMessage object, you can just call the CreateForwareMessage() method:
var forwareMessage = item.CreateForward();
Regarding the other question: Get the MIME content of the mail and attach it to a new message:
item.Load(new PropertySet(BasePropertySet.IdOnly, ItemSchema.MimeContent));
var mail = new EmailMessage(service);
var attachment = mail.Attachments.AddFileAttachment("Original message.eml", item.MimeContent.Content);
attachment.ContentType = string.Format("message/rfc822; charset={0}", item.MimeContent.CharacterSet);
mail.ToRecipients.Add("hkrause#infinitec.de");
mail.Subject = "testmail";
mail.SendAndSaveCopy();
EDIT:
Create forward message and set reply to header:
var fw = item.CreateForward();
var fwMsg = fw.Save(WellKnownFolderName.Drafts);
fwMsg.ReplyTo.Add("personA#company.com");
fwMsg.SendAndSaveCopy();

Resources